Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package grocerybagclient; import java.util.LinkedList; import java.util.Listiterator, public class GroceryBag { private LinkedList bag; public Grocery BagOX bag = new LinkedList0); } public LinkedList getBagOX return

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

package grocerybagclient; import java.util.LinkedList; import java.util.Listiterator, public class GroceryBag { private LinkedList bag; public Grocery BagOX bag = new LinkedList0); } public LinkedList getBagOX return bag: } public boolean add(String newitem){ return bag.add(newitem); } public String remove(String anltemX if(bag.remove(anltem)) return anltem; else return null; } public void clear({ bag.clear(); public boolean contains(String anltemX return bag.contains(anltem); } public int count(String anltem/ LinkedList tempBag = new LinkedList->(bag); int elementcount = 0; while(tempBag.remove(anitem) == trueX elementcount++; } return elementcount; } public int getSize(X return bag.size(); } public boolean isEmptyOX return bag.isEmptyO; } public void display if(lisEmptyOX ListIterator bagiterator = bag.listiterator(0); while(baglterator hasNext(X System.out.println(bagiterator.next(); } } else{ System.out.println("Bag is empty"); } } public GroceryBag union(GroceryBag anotherBagX GroceryBag newBag = new GroceryBag(); if(lisEmptyOX Listiterator baglterator = bag.listiterator(0); while(baglterator.hasNextOX newBag.add(baglterator.next().toString(); } } if(!anotherBag.isEmptyOX Listiterator anotherBaglterator = anotherBag.getBag().listiterator(); while(anotherBaglterator hasNextOX newBag.add(anotherBaglterator.next().toString); } } return newBag } public GroceryBag intersection(GroceryBag anotherBag GroceryBag newBag = new GroceryBag(); if(lisEmptyOX Listiterator baglterator = bag.listiterator(0); while(bagiterator hasNext(X String item = baglterator.next().toStringo: if(anotherBag.contains(item)X newBag.add(item); } } } return newBag; } public GroceryBag difference(GroceryBag anotherBag/ GroceryBag newBag = new GroceryBago; if(lisEmptyOX ListIterator baglterator = bag.listiterator(0); while(baglterator.hasNext(X String item = baglterator.next().toString; if(!anotherBag.contains(item)X newBag.add(item); } } return newBag; } } package grocerybagclient; import java.util.Scanner; public class GroceryBagClient { private static int getMenuChoice(Scanner input/ System.out.println("Please choose one of the following:"); System.out.println(" ."); System.out.println("1) Add item to a bag"); System.out.println("2) Remove an item from a bag"); System.out.println("3) Clear the contents of a bag"); System.out.println("4) Find an item in a bag"); System.out.println("5) find the quantity of an item in a bag"); System.out.println("6) Find the total number of items in a bag"); System.out.println("7) Display the contents of a bag"); System.out.println("8) Create the union bag"); System.out.println("9) Create the intersection bag"); System.out.println("10) Create the difference bag that has items of bag1 not in bag2"); System.out.println("11) Exit"); user input and try until it finds valid input do{ System.out.print("Enter your choice (1-11): "); String choiceString = input.nextLine(); Try{ int choice = Integer.parseInt(choiceString.trim()); if(choice > 0 && choice 0 && choice

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

More Books

Students also viewed these Databases questions

Question

How could burnout have a good side? What may be a benefit?

Answered: 1 week ago