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

(2) The GroceryBag Class Implement a class called Groce that represents a bag wh

ID: 3787242 • Letter: #

Question

(2) The GroceryBag Class Implement a class called Groce that represents a bag which will hold Groceryltem objects. The class has the following private static constants: MAXWEIGHT of type double which indicates the maximum weight that the bag can hold (set it to 5 Kg). Make use of this value property in the code below. MAK ITEMS of type integer that indicates the maximum number of items that can be placed in the bag (set it to 25). Make use of this value properly in the code below. The class has the following private instance variables: tems which is an array that will hold Groceryltem objects. numItems which is the number of items in the bag. weight of type float which returns the total weight of all items currently in the bag. Create the following instance methods: Appropriate public get methods A zero-argument constructor. Atostring0 method that shows the number of items in the bag and the total weight of the bag or outputs An empty grocery bag A method called additem0 which adds a given Groce m to the bag only if the total weight of the bag is not exceeded by this item. A method called removeltem (Groceryltem item) which removes the given item from the bag. A heaviestltem( method that returns the heaviest item in the cart. Return null if bag is empty. A has(Groceryltem item) method that returns a boolean indicating whether or not the given item is currently in the bag.

Explanation / Answer


import java.util.*;
public class Grocerybag {
private   int max_wt=5;
private   int max_item=3;
int numitem;
int wt;
List<Integer>alist=new ArrayList<Integer>();
Grocerybag(){
   wt=0;
   numitem=alist.size();
  
}
public void additems(int item){
   alist.add(item);
       wt+=item;
   //System.out.println(wt);
   if(wt<=5 &&alist.size()<=3){
       System.out.println("");
  
   }
   else{
       alist.remove(item);
   wt=wt-item;
   System.out.println("exceeds limit");
   }
   numitem=alist.size();
  
   return;
}
public void removeitem(int item){
   for(int i=0;i<alist.size();i++){
       if(alist.contains(item));{
       alist.remove(i);
       wt=wt-item;
       numitem=alist.size();
       }
   }
  
}
public void heaviestitem(){
   int big=0;
   for(int i=0;i<alist.size();i++){
       if(alist.get(i)>big)
           big=alist.get(i);
      
   }
   System.out.println("heaviest object is"+big);
}
public boolean has(int item){
   if(alist.contains(item))
   return true;
   else
       return false;
  
}
public String tostring(){
   return "numberofitem is"+numitem+" and weight is:"+wt;
}
public void display(){
   int val=0;
   for(int i=0;i<alist.size();i++)
       val+=alist.get(i);
       System.out.print(val+"val ");
}
}
import java.util.Scanner;
public class example {


public static void main(String[] args) {
  
   Grocerybag b=new Grocerybag();
   b.additems(1);
   b.additems(2);
   b.additems(1);
   // b.display();
     
   System.out.println(b.has(2));
  
}

}