Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java data structure - the previous hw is in the pics This homework includes two questions, Q1 is mandatory and Q2 is a bonus. Q1.

java data structure - the previous hw is in the pics image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
This homework includes two questions, Q1 is mandatory and Q2 is a bonus. Q1. In the previous homework, you wrote a GroceryBag application. You will use your code to add more features to your application. Your application should now support new operation: Undo. As the name of the operation suggest, Undo will undo what ever operation the user performed before it. Use an array stack to implement the undo operation. Note that Undo can only be applied to: 1) Add item to the bag 2) Remove an item from the bag Also, you will work with only one bag, so you need an undo stack for bag 1 only. Undo operation has a limit of only 5 operations. If your stack fills up, remove the oldest operation (shift the elements to the left to make room for the new value). Hint: since you need to undo two operations (add item, remove item) then your stack should store both the element and the operation (add or remove) to be able to undo the previous operation correctly. For example: if the user performed the following Add item: milk egg Add item: muffins Add item: Strawberry the content of the bag will be: milk egg muffins Strawberry Undo the content of the bag will be: milk egg muffins Undo the content of the bag will be: milk egg remove: milk the content of the bag will be: egg Undo the content of the bag will be: egg milk The menu for your GroceryBag application should change to: 1) Add item to the bag 2) Remove an item from the bag 3) Display the content of the bag 4) Undo 5) Exit Note: If add item, remove item, and print methods from last homework have errors, fix them first. The other methods from the previous homework are not needed so you don't need to worry about them. Write a program to test undo and redo. can LTE prolom this is not needed. Part 2,3,4 import java.util.Scanner; class Item String data; Item next; Item(String data) { this.data=data; } class GroceryBag Item head; static int size=0; GroceryBago { head=null; size=0; } public boolean add(String newitem) { Item item=new Item(newitem); size++; if(head==null) { head=item; return true; } Item temp=head; while(temp.next!=null) { temp=temp.next; } if(temp==null) return false; temp.next=item; return true; ) // remove method public String remove(String anltem) { Item pre=null,temp: temp=head LTE 7:06 fremove method public String remove(String anltem) { Item pre=null,temp; temp=head; // search for required item in groceryBag while(temp!=null) { if(temp.data.equals(anitem)) { size- if (pre==null) // if the first item is matched { head=head. next; return anltem; } else { pre.next=temp.next; return anltem; } } pre=temp; temp=temp.next; } return null; ) // clear method public void clear() { head=null; size=0; } | Contains Method public boolean contains (String anltem) { Item temp=head; while(temp!=null) { if(temp.data.equals(anltem)) return true; temp=temp.next; } LTE 7:06 return true; temp=temp.next; } return false; } //count method public int count(String anltem) { int count=0; Item temp=head; while(temp!=null) { if(temp.data.equals(anltem)) count++; temp=temp.next; } return count; } // getSize() public int getSize() { return size; } // isEmpty public boolean isEmpty() { return sizes=0; } public void display { Item temp=head; while(temp!=null) { System.out.println(temp.data); temp=temp.next; } } // Union public GroceryBag union(Grocery Bag another Bag) { LTE 7:06 // Union public GroceryBag union (GroceryBag another Bag) { // for doing union we insert all item of first Il grocery item into result and for second we check that // this is available or not in the result if not present add it GroceryBag result=new GroceryBago: result.head=head; Item t2=another Bag.head; while(t2!=null) { if(!contains(t2.data)) { result.add(t2.data); } t2=t2.next; } return result; } // Intersection public GroceryBag intersection (GroceryBag anotherBag) { GroceryBag result=new GroceryBago: Item t2=anotherBag.head; // search second bag item in first if it is available add it to the result while(t2!=null) { if(contains(t2.data)) { result.add(t2.data); } t2=t2.next; } return result 1:06 LTE // Difference public GroceryBag difference(GroceryBag anotherBag) { GroceryBag result=new GroceryBago: Item temp=head; while(temp!=null) { if(!anotherBag.contains(temp.data)) { result.add(temp.data); } temp=temp.next; } return result; } public class Chegg110801 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); GroceryBag bag1=new GroceryBag(); GroceryBag bag2=new GroceryBag(); System.out.println("Hello, welcome to Food Mania Grocery Store."); while(true){ System.out.println("Please choose one of the following:"); System.out.println("- System.out.println("1) Add item to a bag 2)Remove an item from a bag 3) Clear the contents of a bag"); System.out.println("4)Find an item in a bag 5)Find the quantity of an item in a bag"); System.out.println("6) Find the total number of items in a bag 7)Display the contents of a bag"); System.out.printin("8)Create the union Bag|n9) Create the intersection Bag"); System.out.println("10) Create the difference Bag 11)Exit"); Bag 11)Exit"); 7:06 int choice=sc.nextInt(); LTE switch(choice) { case 1: System.out.println("Which bag you want to add 1 or 2"); int c=sc.nextInt(); System.out.println("Enter the items do you want"); if(c==1) { String str=""; while(true) { str=sc.next(); if(str.equals("$")) { break; } bag1.add(str); } } else { String str="" while(true) { str=sc.next(); if(str.equals("*")) { break; } bag2.add(str); } 3 break; case 2: String str=sc.next(); bag1.remove(str); break; case 3: bag1.clear(); break; case 4: str=sc.next(); hant contains sin 7:06 LTE str=sc.next(); if(str.equals("$")) { break; bag2.add(str); } } break; case 2: String str=sc.next(); bag1.remove(str); break; case 3: bag1.clear(); break; case 4: str=sc.next(); bag1.contains(str); break; case 5: str=sc.next(); int count=bag1.count(str); break; case 6: count=bag1.getSize(); break; case 7: bag1.display(); break; case 8: GroceryBag union=bag1.union(bag2); break; case 9: GroceryBag intersection=bag1.intersection(bag2); break; case 10: GroceryBag diff=bag1.difference(bag2); break; case 11: System.exit(0); }

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions

Question

What physical locations do you work in?

Answered: 1 week ago