Question: Add an replaceIfDifferent method to the Box class. The method takes in an object. If the object is different from what is already in the

Add an replaceIfDifferent method to the Box class. The method takes in an object. If the object is different from what is already in the box, replace the object and return true. If the object is the same as what is already in the box, return false.

public class Box { private T thing; private int thingCount; public Box(T firstThing) { this.thing = firstThing; this.thingCount = 1; } public T getContents() { return thing; } public int getCount() { return thingCount; } public void replaceContents(T newThing) { this.thing = newThing; thingCount++; } @Override public String toString() { return thing.toString() + " (item " + thingCount + ")"; } @Override public boolean equals(Object other) { if(other instanceof Box) { Box otherBoxR = (Box) other; boolean sameThing = this.thing.equals(otherBoxR.thing); boolean sameCount = this.thingCount==otherBoxR.thingCount; return sameThing && sameCount; } else { return false; } }

}

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!