Question: Need help with Java error code. So, the problem overview is the project will simulate a simple computer system consisting of a CPU and Memory.

Need help with Java error code. So, the problem overview is the project will simulate a simple computer system consisting of a CPU and Memory. The CPU and Memory will be simulated by separate processes that communicate. Memory will contain one program that the CPU will execute and then the simulation will end. Which I keep getting this error:

Usage: java Memory [input-file] Exception in thread "main" java.util.NoSuchElementException: No line found

CPU code:

public class Project

{

private static final int USER_MODE_START_ADDRESS = 1000;

private static final int KERNEL_MODE_START_ADDRESS = 2000;

public static void main(String[] args)

{

if (args.length

System.err.println("Not enough parameters! Try Again.");

System.exit(1);

}

String pInput = args[0];

int timeout = Integer.parseInt(args[1]);

Runtime runtime = Runtime.getRuntime();

try {

Process memoryProcess = runtime.exec("java Memory " + pInput);

final InputStream errorStream = memoryProcess.getErrorStream();

new Thread(() ->

{

byte[] buffer = new byte[8192];

int length = -1;

try

{

while ((length = errorStream.read(buffer)) > 0)

{

System.err.write(buffer, 0, length);

}

} catch (IOException e)

{

e.printStackTrace();

}

}).start();

Scanner memoryInput = new Scanner(memoryProcess.getInputStream());

PrintWriter memoryOutput = new PrintWriter(memoryProcess.getOutputStream());

CPU cpu = new CPU(memoryInput, memoryOutput, timeout);

cpu.run();

} catch (IOException e)

{

e.printStackTrace();

System.err.println("Failed to create a new operation.");

System.exit(1);

}

}

private static void message(Object... str)

{

}

private static class CPU {

private int PC, SP, IR, AC, X, Y;

private int timer, timeout;

private Scanner memoryInput;

private PrintWriter memoryOutput;

private boolean kernelMode;

public CPU(Scanner memoryInput, PrintWriter memoryOutput, int timeout)

{

this.timeout = timeout;

this.memoryInput = memoryInput;

this.memoryOutput = memoryOutput;

this.kernelMode = false;

this.PC = this.IR = this.AC = this.X = this.Y = this.timer = 0;

this.SP = USER_MODE_START_ADDRESS;

}

private void fetch()

{

IR = readMemory(PC++);

}

private void push(int data)

{

writeMemory(--SP, data);

}

private int pop()

{

return readMemory(SP++);

}

public void run()

{

boolean running = true;

while (running)

{

fetch();

running = instructionExecute();

timer++;

if (timer >= timeout && !kernelMode)

{

timer = 0;

kernelMode();

PC = KERNEL_MODE_START_ADDRESS;

}

}

}

private void kernelMode()

{

message("Kernel mode.");

kernelMode = true;

int tempSP = SP;

SP = KERNEL_MODE_START_ADDRESS - 1;

push(tempSP);

push(PC);

push(IR);

push(AC);

push(X);

push(Y);

}

private int readMemory(int address) {

if (address >= USER_MODE_START_ADDRESS && !kernelMode)

{

System.err.println("Memory is in user mode");

System.exit(-1);

}

memoryOutput.println("r"+address);

memoryOutput.flush();

return Integer.parseInt(memoryInput.nextLine());

}

private void writeMemory(int address, int data)

{

memoryOutput.printf("w%d,%d ", address, data);

memoryOutput.flush();

}

private void endMemoryProcess()

{

memoryOutput.println("e");

memoryOutput.flush();

}

(rest of the code is irrelevant to this issue)

Memory code:

Need help with Java error code. So, the problem overview is the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!