Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help with first year computer science coding (java) (10 marks) P1: Memory

ID: 3876764 • Letter: P

Question

please help with first year computer science coding (java)

(10 marks) P1: Memory and Terminal classes Focus: basics of classes and objects To simulate the computer, we need to create classes representing each of its components (Registers in CPU, Memory, I/O, etc.) The objective of this laboratory will be to implement the Memory class, which will simulate the memory of the computer, as well as a Terminal class to simulate the Input/Output between the simulated computer and the screen/keyboard. MEMORY 0 0799- Input Device 2 3 4 5 6 7 8 9 0798 0198 0499 1008 1108 0899 0909 0898 000O Program Counter CENTRAL PROCESSING UNIT Instruction Register Accumulator Output Device After you finish Labs P1 to P4, your program should run as in the example below. The default program loaded in the memory above (i.e. the red values in the memory) will read two integers from the user and display the larger of them

Explanation / Answer

Memory.java : ------------------------>>>>>>>>>>>>>

import java.util.Random;

public class Memory{
private int size;
private int[] content;

public Memory(int s){
  size = s;
  content = new int[size];
}

public void setValue(int adress,int value){
  if(adress > size-1 || adress < 0){
   Terminal.output("adress is not valid");
  }else{
   content[adress] = value;
  }
}

public int getValue(int adress){
  if(adress > size || adress < 0){
   return -1;
  }

  return content[adress];
}

public void showContent(){
  Terminal.output("Showing Content of memory unit");
  for(int i = 0;i<size;i++){
   Terminal.output(content[i]);
  }
}

public void loadMemory(){
  Random r = new Random();
  for(int i = 0;i<size;i++){
   setValue(i,r.nextInt());
  }
}
}

Terminal.java :--------------------->>>>>>>>>

import java.util.Scanner;

public class Terminal{
public static int input(){
  int ret;
  Scanner sc = new Scanner(System.in);
  System.out.print(" ?");
  ret = sc.nextInt();
  return ret;
}

public static void output(String msg){
  System.out.print(" OUTPUT : "+msg);
}
public static void output(int msg){
  System.out.print(" OUTPUT : "+msg);
}
}

import java.util.Scanner;

public class Terminal{
public static int input(){
  int ret;
  Scanner sc = new Scanner(System.in);
  System.out.print(" ?");
  ret = sc.nextInt();
  return ret;
}

public static void output(String msg){
  System.out.print(" OUTPUT : "+msg);
}
public static void output(int msg){
  System.out.print(" OUTPUT : "+msg);
}
}

Test.java :----------------->>>>>>>>>>>>>

public class Test{
public static void main(String[] args) {
  Memory m1 = new Memory(100);
  m1.loadMemory();
  m1.setValue(45,Terminal.input());
  m1.showContent();
}
}