Question

import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) {...

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
int programCounter = 0;
List<String> program = new ArrayList<String>();
Stack<String> stack = new Stack<String>(); // TODO string probably not best
program.add("GOTO start<<1>>");
program.add("LABEL Read");
program.add("LINE -1");
program.add("FUNCTION Read -1 -1");
program.add("READ");
program.add("RETURN ");
program.add("LABEL Write");
program.add("LINE -1");
program.add("FUNCTION Write -1 -1");
program.add("FORMAL dummyFormal 0");
program.add("LOAD 0 dummyFormal");
program.add("WRITE");
program.add("RETURN ");
program.add("LABEL start<<1>>");
program.add("LINE 1");
program.add("FUNCTION main 1 4");
program.add("LIT 0 i");
program.add("LIT 0 j");
program.add("LINE 2");
program.add("LOAD 0 i");
program.add("LOAD 1 j");
program.add("BOP +");
program.add("LIT 7");
program.add("BOP +");
program.add("STORE 0 i");
program.add("LINE 3");
program.add("LOAD 0 i");
program.add("ARGS 1");
program.add("CALL Write");
program.add("STORE 1 j");
program.add("POP 2");
program.add("HALT");
while(!program.get(programCounter).equals("HALT")){
System.out.println(program.get(programCounter));
programCounter++;
}
// TODO run the program and print every stack after each step
}
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the modified code below.

CODE

import java.util.ArrayList;

import java.util.List;

import java.util.Stack;

public class Main {

public static void main(String[] args) {

int programCounter = 0;

List<String> program = new ArrayList<String>();

Stack<String> stack = new Stack<String>();

program.add("GOTO start<<1>>");

program.add("LABEL Read");

program.add("LINE -1");

program.add("FUNCTION Read -1 -1");

program.add("READ");

program.add("RETURN ");

program.add("LABEL Write");

program.add("LINE -1");

program.add("FUNCTION Write -1 -1");

program.add("FORMAL dummyFormal 0");

program.add("LOAD 0 dummyFormal");

program.add("WRITE");

program.add("RETURN ");

program.add("LABEL start<<1>>");

program.add("LINE 1");

program.add("FUNCTION main 1 4");

program.add("LIT 0 i");

program.add("LIT 0 j");

program.add("LINE 2");

program.add("LOAD 0 i");

program.add("LOAD 1 j");

program.add("BOP +");

program.add("LIT 7");

program.add("BOP +");

program.add("STORE 0 i");

program.add("LINE 3");

program.add("LOAD 0 i");

program.add("ARGS 1");

program.add("CALL Write");

program.add("STORE 1 j");

program.add("POP 2");

program.add("HALT");

while(!program.get(programCounter).equals("HALT")){

System.out.println(program.get(programCounter));

programCounter++;

}

System.out.println(stack);

}

}

OUTPUT

GOTO start<<1>>

LABEL Read

LINE -1

FUNCTION Read -1 -1

READ

RETURN

LABEL Write

LINE -1

FUNCTION Write -1 -1

FORMAL dummyFormal 0

LOAD 0 dummyFormal

WRITE

RETURN

LABEL start<<1>>

LINE 1

FUNCTION main 1 4

LIT 0 i

LIT 0 j

LINE 2

LOAD 0 i

LOAD 1 j

BOP +

LIT 7

BOP +

STORE 0 i

LINE 3

LOAD 0 i

ARGS 1

CALL Write

STORE 1 j

POP 2

[]

Add a comment
Know the answer?
Add Answer to:
import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

  • Evaluateeg EvaluateInFix.java: import java.util.Stack; import java.util.Scanner; public class EvaluateInfix {    public static void main(String[] args)...

    Evaluateeg EvaluateInFix.java: import java.util.Stack; import java.util.Scanner; public class EvaluateInfix {    public static void main(String[] args)    {    Scanner keyboard = new Scanner(System.in); System.out.println("This program an inFix Expression: "); System.out.print("Enter an inFix Expression that you want to evaluate (or enter ! to exit): "); String aString = keyboard.nextLine();    while (!aString.equals("!")) {    System.out.println (evaluateinFix(aString));    System.out.print("Enter an inFix Expression that you want to evaluate (or enter ! to exit): ");    aString = keyboard.nextLine(); } // end while...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr =...

    import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr = new Scanner(System.in); // TODO: get user choice    // TODO: call printTable method passing choice as the parameter    } public static void printTable(int stop) { // TODO: print header    // TODO: loop to print table rows up to stop value    } Write a Java program where the main () method prompts the user to select an integer value between 1 and...

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • package com.snhu.sslserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SslServerApplication { public static void main(String[] args)...

    package com.snhu.sslserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SslServerApplication { public static void main(String[] args) { SpringApplication.run(SslServerApplication.class, args); } } //FIXME: Add route to enable check sum return of static data example: String data = "Hello World Check Sum!";

  • /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {   ...

    /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {    /**    * @param args    */    public static void main(String[] args) {        List groceryList = new ArrayList<>();        groceryList.add("rice");        groceryList.add("chicken");        groceryList.add("cumin");        groceryList.add("tomato");        groceryList.add("cilantro");        groceryList.add("lime juice");        groceryList.add("peppers");               groceryList.remove("cilantro");               for (int i = 0; i            if (groceryList.get(i).equals("peppers")) {...

  • Java has a class for a stack. It is java.util.Stack. Let us make use of this...

    Java has a class for a stack. It is java.util.Stack. Let us make use of this class. Step 1: Create a project. Name the project MyStk. This will result in the creation of the main class called MyStk if you are using Netbeans IDE with relevant default settings unchanged. If comments are excluded you should see the following in the code editor after creating the project. package mystk; public class MyStk { public static void main(String[ ] args){ } }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT