Question: (A) The Memory class: Create a public class called Memory which represents the memory unit in our computer simulator and that contains: o A private
(A)
The Memory class: Create a public class called Memory which represents the memory unit in our computer simulator and that contains:
o A private attribute called contents of an array of integers. This attribute will reference an array object that holds the contents of our memory unit. o A private attributed called size that represents the size of the memory unit.
o A one-argument constructor that sets size to a given value and creates an array with the given size for contents.
o The following setter and getter methods:
getValue(int address) to read the value at a given memory address
setValue(int address, int value) to write at a given memory address.
Both of the above methods should check the validity of the address. If not valid (i.e. not in the range from 0 to (size-1) inclusive, an error message should be displayed (and the value -1 should be returned for the getter method).
o A method showContents which displays the contents of the memory unit.
o A method LoadMemory to initializes the array with the values provided in the above example. These values correspond to a default program downloaded into the memory.
(B)
Create a class Terminal that has three static methods:
o input() that returns an integer value read from the keyboard. The method should first print the question mark (?), wait for a value, and then return it.
o output(String msg) that prints on the screen the word OUTPUT followed by a colon (:) then the value of msg.
o output(int msg) same as above except that it receives integer arguments.
(C)
Testing: Create a class called Test that include code to test your classes and ensure that your code works properly. For example, create a memory object with size =100, load the default program, set a value at any address, and finally display the memory contents. Also, invoke the methods of the Terminal class without creating any objects in your test program.
HERE IS MY CODE: CAN SOMEONE EXPLAIN WHAT I AM DOING WRONG?
(A)
import java.util.Arrays;
import java.util.Scanner;
public class Memory {
private int[] contents;
private int size;
public Memory(int size){
this.size=size;
contents = new int[size];
}
public int getValue(int address){
return contents[address];
}
public void setValue(int address, int value){
if ((address>=0) && (address<=(size-1))){
contents[address] = value;
}else{
System.out.println("Error!");
}
}
public void showContents(){
System.out.println(Arrays.toString(contents));
}
public void LoadMemory(int numValues){
Scanner input = new Scanner(System.in);
System.out.print("Enter " + numValues + " values: ");
for (int i = 0; i < numValues; i++)
contents[i] = input.nextInt();
}}
(B)
import java.util.Scanner;
public class Terminal {
public static double input(){
Scanner input = new Scanner(System.in);
System.out.print("? ");
double v = input.nextDouble();
return v;
}
public static void output(String msg){
System.out.println("OUTPUT: "+ msg);
}
public static void output(int msg){
System.out.println("OUTPUT: "+ msg);
}
}
(C)
public class Test {
public static void main(String[] args) {
Memory t1 = new Memory(100);
t1.LoadMemory(10);
t1.setValue(3, 021);
t1.showContents();
input(); //from terminal class
output(happiness);
output(12);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
