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 | |
} | |
} |
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
[]
import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { 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) { 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) { 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) { 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 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 = 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...
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 { /** * @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")) {...
Priority Queue Demo import java.util.*; public class PriorityQueueDemo { public static void main(String[] args) { //TODO 1: Create a priority queue of Strings and assign it to variable queue1 //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1 System.out.println("Priority queue using Comparable:"); //TODO 3: remove from queue1 all the strings one by one // with the "smaller" strings (higher priority) ahead of "bigger"...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...