Given the Generic class, public class GenericMemoryCell<AnyType> { public AnyTyp
ID: 3846555 • Letter: G
Question
Given the Generic class,
public class GenericMemoryCell<AnyType>
{
public AnyType read()
{ return storedValue; }
public void write(AnyType x)
{ storedValue = x; }
private AnyType storedValue;
}
public class TestGenericMemoryCell
{
public static void main(String [] args)
{
GenericMemoryCell<Integer> m =
new GenericMemoryCell<Integer> ( );
m.write( 37 );
int val = m.read();
System.out.println(“Cell contents: “ + val );
}
}
Change his GenericMemoryCell class to store two items, which will be referred to
as storedValueA and storedValueB.
It should have the following constructor and methods. The methods should
all operate on the object making the call (none are static).
a. constructor
receives two parameters and stores them
b. readA
returns storedValueA
c. readB
returns storedValueB
d. writeA
receives a parameter and sets storedValueA
e. writeB
receives a parameter and sets storedValueB
f. swap
exchanges the two stored values.
g. contains
receives a parameter and returns true if it is equal to either of the
stored values
h. valuesAreSame
returns true if the stored values equal each other
i. matches
receives another GenericMemoryCell as a parameter, and returns true if
both of its stored values can be found in the stored values of the current
GenericMemoryCell (order of stored values is not important)
j. toString
formats the GenericMemoryCell as [storedValueA, storedValueB]
k. main
demonstrate each of your methods in the order shown above, and where
applicable, show both true and false outcomes (be sure to clearly label
the output so that the grader can follow it)
Explanation / Answer
Hi, Below is your program: -
GenericMemoryCell.java
public class GenericMemoryCell<AnyType> {
public GenericMemoryCell(AnyType storedValueA, AnyType storedValueB) {
this.storedValueA = storedValueA;
this.storedValueB = storedValueB;
}
public GenericMemoryCell() {
}
public AnyType readA() {
return storedValueA;
}
public void writeA(AnyType x) {
storedValueA = x;
}
public AnyType readB() {
return storedValueB;
}
public void writeB(AnyType x) {
storedValueB = x;
}
public void swap() {
AnyType temp = this.storedValueA;
this.storedValueA = this.storedValueB;
this.storedValueB = temp;
}
public boolean contains(AnyType B) {
if (this.storedValueA.equals(B) || this.storedValueB.equals(B)) {
return true;
}
return false;
}
public boolean valuesAreSame() {
if (this.storedValueA.equals(this.storedValueB)) {
return true;
}
return false;
}
public boolean matches(GenericMemoryCell<AnyType> obj) {
if (this.contains(obj.readA()) && this.contains(obj.readB())) {
return true;
}
return false;
}
@Override
public String toString() {
return "["+storedValueA +","+ storedValueB +"]";
}
public static void main(String[] args) {
GenericMemoryCell<Integer> cell = new GenericMemoryCell<Integer>(24, 56);
GenericMemoryCell<Integer> cell1 = new GenericMemoryCell<>();
System.out.println("Cell 1 elements are: "+cell);
cell1.writeA(40);
cell1.writeB(40);
System.out.println("Cell 2 elements are: ["+cell1.readA()+","+cell1.readB()+"]");
cell.swap();
System.out.println("Cell 1 elements after swapping are : "+cell);
System.out.println();
System.out.println("Does cell 1 contains 40? "+cell.contains(40));
System.out.println("Does cell 2 contains 40? "+cell1.contains(40));
System.out.println();
System.out.println("Does cell 1 values are same? "+cell.valuesAreSame());
System.out.println("Does cell 2 values are same? "+cell1.valuesAreSame());
System.out.println();
System.out.println("Is cell 1 and cell 2 are same? "+cell.matches(cell1));
System.out.println();
GenericMemoryCell<Integer> cell2 = new GenericMemoryCell<Integer>(24, 56);
System.out.println("Is cell 1 and cell 3 are same? "+cell.matches(cell2));
}
private AnyType storedValueA;
private AnyType storedValueB;
}
Sample Run: -
Cell 1 elements are: [24,56]
Cell 2 elements are: [40,40]
Cell 1 elements after swapping are : [56,24]
Does cell 1 contains 40? false
Does cell 2 contains 40? true
Does cell 1 values are same? false
Does cell 2 values are same? true
Is cell 1 and cell 2 are same? false
Is cell 1 and cell 3 are same? true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.