Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bag class had two member variables one was called size (int type) and the other

ID: 3800336 • Letter: B

Question

Bag class had two member variables one was called size (int type) and the other was item (type T). You must modify that class to have four member variables called size, item1, item2 and item3. The add method needs to be modified as well. If size is zero, item1 should refer to the item and size needs to be incremented. On the other hand if size is one, item2 should refer to the item and size needs to be incremented. Finally if size is 3, a message should be displayed by the add method saying the bag is full.

You must also write a toString method and test all the methods of the class. The interface must be written as well and main should implement that interface.

Bag Interface java x 2 public interface Bag Interface f 3 public void display() 4 public void ad (T it);

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

public class Bag<T> implements BagInterface<T> {

   private int size;

   private T item1, item2, item3;

   public Bag() {

       size = 0;

   }

   @Override

   public void display() {

       if(size == 0)

           System.out.println("Empty Bag");

       else if(size == 1)

           System.out.println(item1);

       else if(size == 2){

           System.out.println(item1);

           System.out.println(item2);

       }else{

           System.out.println(item1);

           System.out.println(item2);

           System.out.println(item3);

       }

   }

   @Override

   public void add(T t) {

       if(size == 3){

           System.out.println("Bag is full");

           return;

       }

       if(size == 0){

           item1 = t;

       }else if(size == 1){

           item2 = t;

       }else{

           item3 = t;

       }

      

       size++;

   }

   @Override

   public String toString() {

       String result = "";

       if(size == 0)

           result = "Bag is empty";

       else if(size == 1)

           result = "Item1: "+item1;

       else if(size == 2){

           result = "Item1: "+item1+", Item2: "+item2;

       }else{

           result = "Item1: "+item1+", Item2: "+item2+", and Item3: "+item3;

       }

       return result;

   }

   public static void main(String[] args) {

       // Ceating object

       BagInterface<Integer> bag = new Bag<>();

       System.out.println(bag);

       bag.add(4);

       System.out.println(bag);

       bag.add(2);

       System.out.println(bag);

       bag.add(1);

       System.out.println(bag);

       bag.add(9);

       System.out.println(bag);

   }

}

/*

Sample run:

Bag is empty

Item1: 4

Item1: 4, Item2: 2

Item1: 4, Item2: 2, and Item3: 1

Bag is full

Item1: 4, Item2: 2, and Item3: 1

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote