Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions