Question
java data structure - the previeous hw grocerybag is in the pics bellow- without importing anything except scanner please public class Grocerybag { item head;
java data structure - the previeous hw grocerybag is in the pics bellow- without importing anything except scanner please
public class Grocerybag { item head; int size=0; Grocerybag(){ head=null; size=0; } public boolean add(String newitem) { item itm=new item(newitem); size++; if(head==null) { head=itm; return true; } item temp=head; while(temp.next!=null) { temp=temp.next; }//while if(temp==null) return false; temp.next=itm; return true; }//add method
public String remove(String item) { item pre=null; item temp = null; while(temp!=null) { if(temp.data.equals(item)) { size--; if(pre==null) { head=head.next; return item; }//if else { pre.next=temp.next; return item; }//else }//if pre=temp; temp=temp.next; }//while return null; }//remove method
public void clear() { head=null; size=0; }//clear method
public boolean contains(String item) { item temp=head; while(temp!=null) { if(temp.data.equals(item)) return true; temp=temp.next; }//while return false; }//contains method
public int count(String item) { int count=0; item temp=head; while(temp!=null) { if(temp.data.equals(item)) count++; temp=temp.next; }//while return count; }//count method
public int getsize() { return size; }//get size method
public boolean isEmpty() { return size==0; }//isempty method
public void display() { item temp=head; while(temp!=null) { System.out.println(temp.data); temp=temp.next; }//while }//display method
public Grocerybag union(Grocerybag anotherbag) { Grocerybag result =new Grocerybag(); result.head=head; item t2=anotherbag.head; while(t2!=null) { if(!contains(t2.data)) { result.add(t2.data); }//if t2=t2.next; }//while return result; }//union
public Grocerybag itersection(Grocerybag anotherbag) { Grocerybag result =new Grocerybag(); item t2=anotherbag.head; while(t2!=null) { if(contains(t2.data)) { result.add(t2.data); }//if t2=t2.next; }//while return result; }//intersection
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 Unda 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. doubly linked has same feature except in doubly w can traverse in both the direction but for this problem this is not needed Part 2.3.4 import java.util.Scanner: Elastem String data, Item next ItemString data) this.data-data; 2 class Grocery Bao Item head staticities GroceryBago { head=null si > public boolean addString newem) Item tem new iteminewitem: siz heade nuit 1 head=item; return true, ) Item temanhead while(tomp.extenull) 1 temamp.net 1 itempat return false temp.next=item; return true; remove method public String remove(String antem empre ultem: tempuhead: Search for required tem in grocery Bag whiletomaten item data qualsaniemi ifre null) if the first femismatched headhead.net return anter > else pre nextstemp.next; return antem 2 prestem tempstemp.net 1 return null clear method public void card teadnult size Tad Garcia Method public containing fount med tem re DOS mi is pcccani Polic as Saptap emmat Symo proposal rocery more for doing ceyrando recht Ocena w Grocery medad that be recerca Grocery manerad carini rence w he Diary Bow Broca the continutul reddet 1 gublic class Cher public void mao Scanners Scarvest Grocery bagten Grecia Grocery Bay Date Orecer Secto Grocery w System.out.prinese Oystem System Ademia ve anatambah of a Systems the to System.Date many the content Syntheon RICE Seminte de watch Synowie we freemouth CORO be og Strings 1 bag. brk Badea w bago str=sc.next(): if(str.equals("S"> . M break; 1 bag1.add(str); 2 else String str=" while(true) stresc.next; if(str.equals("")) break; 1 bag2.add(str): 2 > break; case 2: String stresc.next(); bag1.remove(str); break; case 3: bagi.clear(); break; case 4: strasc.next: bag1.contains(str); break case 5: strasc.next(): int count-bagi.count(str); break; case 6: count=bag1.getSize(); break; case 7: bag1.display: break; case 8: GroceryBag union-bagt.union(bag): break; case 9: GroceryBag intersection-bag1.intersection(bag2); break; case 10: GroceryBag diff-bag 1.difference(bag) break; case 11: System.exit(0); 3 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. 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 Unda 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. doubly linked has same feature except in doubly w can traverse in both the direction but for this problem this is not needed Part 2.3.4 import java.util.Scanner: Elastem String data, Item next ItemString data) this.data-data; 2 class Grocery Bao Item head staticities GroceryBago { head=null si > public boolean addString newem) Item tem new iteminewitem: siz heade nuit 1 head=item; return true, ) Item temanhead while(tomp.extenull) 1 temamp.net 1 itempat return false temp.next=item; return true; remove method public String remove(String antem empre ultem: tempuhead: Search for required tem in grocery Bag whiletomaten item data qualsaniemi ifre null) if the first femismatched headhead.net return anter > else pre nextstemp.next; return antem 2 prestem tempstemp.net 1 return null clear method public void card teadnult size Tad Garcia Method public containing fount med tem re DOS mi is pcccani Polic as Saptap emmat Symo proposal rocery more for doing ceyrando recht Ocena w Grocery medad that be recerca Grocery manerad carini rence w he Diary Bow Broca the continutul reddet 1 gublic class Cher public void mao Scanners Scarvest Grocery bagten Grecia Grocery Bay Date Orecer Secto Grocery w System.out.prinese Oystem System Ademia ve anatambah of a Systems the to System.Date many the content Syntheon RICE Seminte de watch Synowie we freemouth CORO be og Strings 1 bag. brk Badea w bago str=sc.next(): if(str.equals("S"> . M break; 1 bag1.add(str); 2 else String str=" while(true) stresc.next; if(str.equals("")) break; 1 bag2.add(str): 2 > break; case 2: String stresc.next(); bag1.remove(str); break; case 3: bagi.clear(); break; case 4: strasc.next: bag1.contains(str); break case 5: strasc.next(): int count-bagi.count(str); break; case 6: count=bag1.getSize(); break; case 7: bag1.display: break; case 8: GroceryBag union-bagt.union(bag): break; case 9: GroceryBag intersection-bag1.intersection(bag2); break; case 10: GroceryBag diff-bag 1.difference(bag) break; case 11: System.exit(0); 3 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 redoStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started