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

Fully develop the classes for the Linked Implementation of the ADT Bag (i.e., ht

ID: 3751251 • Letter: F

Question

Fully develop the classes for the Linked Implementation of the ADT Bag (i.e., https://github.com/beratgumus/The-Bag-ADT).
Test your classes well (call all methods) before you proceed.

Use the following name for the files: Bag Interface, Linked Bag, and Test Bag.

Automated grocery bag filler

1. Write a program that uses heuristics (that a bagger at a grocery store might use) to fill a shopping bag (an instance of your LinkedBag class) from a whole bunch of grocery items.
2. How you store the initial “bunch” of grocery items is up to you
3. Grocery items’ have properties such as weight, breakability, squishability, etc.
4. As the bag is being filled, items are selected so that roughly (that’s why it’s heuristic and not an algorithm):
4a. The heaviest items (bag of potatoes) are near the bottom (added earlier)
4b. The items that are the most squishable (bread) and breakable (eggs) are near the top (added later).

Explanation / Answer

import java.util.ArrayList;

import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;

class test extends HashBag{
   /**
   *
   */
   private static final long serialVersionUID = 1L;

   public static void main(String args[]){
      
       Bag br, sq, nm;
       br = new HashBag();//breakable item bag
       sq = new HashBag();//squashable item bag
       nm = new HashBag();//normal item bag
      
       GroceryItem potatoes = new GroceryItem("potato", 500, false, true);
       GroceryItem eggs = new GroceryItem("Eggs", 100, true, false);
       GroceryItem breads = new GroceryItem("Bread", 300, false, true);
      
       ArrayList<GroceryItem> ll = new ArrayList<GroceryItem>();
       ll.add(breads);
       ll.add(eggs);
       ll.add(potatoes);
              
       for(GroceryItem ss: ll){
           if(ss.breakable==true){
                   br.add(ss);
           }
           if(ss.squashable==true){
               sq.add(ss);
           }
           nm.add(ss);
       }
   }
  
}
  
class GroceryItem{
   String name;
   float weight;
   boolean breakable, squashable;
   GroceryItem(String name, float weight, boolean breakable, boolean squashable){
       this.name = name;
       this.weight = weight;
       this.breakable = breakable;
       this.squashable = squashable;
   }
}

Implement Bag collection ADT using a linked list:

import java.util.Iterator;

public class Bag<Item> implements Iterable<Item>
{
private Node first;

public class Node
{
public Item item;
public Node next;
}

public void add(Item x)
{
Node oldfirst = first;
first = new Node();
first.item = x;
first.next = oldfirst;
}

public class BagIterator implements Iterator<Item>
{
private Node x = first;

public Item next()
{
Item item = x.item;
x = x.next;
return item;
}

public boolean hasNext()
{ return x != null; }
}

public Iterator<Item> iterator()
{
return new BagIterator();
}

public static void main(String[] args)
{
Bag<Integer> b = new Bag<Integer>();
int[] ints = StdIn.readAllInts();

  
for (int i = 0; i < ints.length; i++)
{ b.add(i); }


StdOut.println("Here are the Integers in your Bag:");
for (int i : b) { StdOut.println(i); }
}
}