1. Engineering
  2. Computer Science
  3. hello there can anyone give me general idea on how...

Question: hello there can anyone give me general idea on how...

Question details

Hello there, Can anyone give me general idea on how generics work in java please? And Modify the Box class from the following code and Add an ArrayList to keep track of the "box history"- all items that have been previously added to the box. Mention the new instance data variable, modified constructor, and the modified replace Contents method.


public class Box<T> {
  
   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;
       }
   }

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution