Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please help with this java assignment. only use java no other language, and only write code where it indicates Your Code Here thank you 5.9
please help with this java assignment. only use java no other language, and only write code where it indicates "Your Code Here" thank you
5.9 LAB: Grocery list editor with undo stack In this lab a grocery list editor with undo functionality is implemented. Step 1: Inspect the UndoCommand abstract base class The read-only UndoCommand.java file has a declaration for the UndoCommand abstract base class. Access UndoCommand.java by clicking on the orange arrow next to LabProgram.java at the top of the coding window. The UndoCommand class represents a command object: an object that stores all needed information to execute an action at a later point in time. For this lab, a command object stores information to undo a grocery list change made by the user. Step 2: Inspect the incomplete GroceryList class The GroceryList class is declared in GroceryList.java. Two protected fields are declared: - An ArrayList of strings for list items - A stack of UndoCommand references for undo commands Note that the addWithUndo() method is already implemented. The method adds a new item to the list and pushes a new RemoveLastCommand object onto the undo stack. Step 3: Implement RemoveLastCommand's execute() method The RemoveLastCommand class inherits from UndoCommand and is declared in RemoveLastCommand.java. When a RemoveLastCommand object is executed, the string ArrayList's last element is removed. So when the user appends a new item to the grocery list, a RemoveLastCommand is pushed onto the stack of undo commands. Popping and executing the RemoveLastCommand then removes the item most recently added by the user. RemoveLastCommand's sourceList field and constructor are already declared: - sourceList is a reference to a GroceryList object's ArrayList of strings. - The constructor takes a reference to an ArrayList of strings as a parameter, and assigns sourceList with the reference. Implement RemoveLastCommand's execute() method to remove sourceList's last element. Step 4: Implement GroceryList's executeUndo0 method Implement GroceryList's executeUndo() method to do the following: - Pop an UndoCommand off the undo stack - Execute the popped undo command File LabProgram.java has code that reads in a list of commands, one per line, that allow for basic testing of basic operations. So after implementing executeUndo0, run your program with the following input: add bananas add grapes add strawberries print undo print undo print quit Verify that the corresponding output is: 0. bananas 1. grapes 2. strawberries 0. bananas 1. grapes 0. bananas The program's output does not affect grading, so additional test cases can be added, if desired. Submitting code written so far to obtain partial credit is recommended before proceeding to the next step. Step 5: Implement the SwapCommand class and GroceryList's swapWithUndo0 method Implement the SwapCommand class in SwapCommand.java. The class itself is declared, but no class members yet exist. Add necessary fields and methods so that the command can undo swapping two items in the grocery list. Implement GroceryList's swapWithUndo0 method. The method swaps list items at the specified indices, then pushes a SwapCommand, to undo that swap, onto the undo stack. Step 6: Implement the InsertAtCommand class and GroceryList's removeAtWithUndo() method Implement the InsertAtCommand class in InsertAtCommand.java. Add necessary fields and methods so that the command can undo removing a grocery list item at an arbitrary index. Implement GroceryList's removeAtWithUndo() method. The method removes the list item at the specified index, then pushes an InsertAtCommand, to undo that removal, onto the undo stack. 459428.3119604.03zqy7 s marked as read only Current file: LabProgram.java - mport java.io.PrintWriter; mport java.util. Scanner; mport java.util. Arraylist; mport java.util. Arrays; ublic class LabProgram \{ public static void main(String ] args) \{ Scanner scnr = new Scanner(System.in); GroceryList groceryList = new Grocerylist(); PrintWriter testFeedback = new PrintWriter(System.out); String command; boolean quit = false; while (!quit) \{ command = scnr nextLine( ); 1/ Process user input if (command. equals("print")) \{ grocerylist.print(testFeedback); \} else if (== command. index0f("add ")) \{ grocerylist. addWithUndo (command. substring(4)); \} else if (== command. index0f( removeat ")) \{ int index = Integer.parseInt(command.substring(9)); grocerylist. removeAtWithUndo (index); \} else if (0== command. index0f("swap ")) \{ int index 1=1, index 2=1; String str = command. substring(5); if (str. index0f(" ") !=1){ index 1= Integer.parseInt (str. substring(,1)); index 2 = Integer.parseInt (str. substring(2,3)); groceryList. swapWithUndo(index1, index2); \} else \{ System. out.print("\"swap \ " command requires two indices, sepc System. out.println("by a space. Ex: swap 2 5"); \} \} else if (command. equals("undo")) \{ if (== groceryList.getUndoStackSize()) \{ System.out.println("Cannot execute undo because undo stack is \} else \{ grocerylist. executeUndo(); \} else if (command. equals("quit")) quit = true; \} else \{ System.out.println("Unknown command: " + command); \} File is marked as read only Current file: UndoCommand.java 1 public abstract class UndoCommand \{ 2 3} public abstract void execute(); 4 Current file: Load default template... Current file: SwapCommand.java Load default template... \begin{tabular}{l|l} 1 & import java.util.Arraylist; \\ 2 & \\ 3 & public class SwapCommand extends UndoCommand \{ \\ 4 & // Your field declarations here \\ 5 & \\ 6 & /l Your constructor code here \\ 7 & \\ 8 & @0verride \\ 9 & public void execute() \{ \\ 10 & // Your code here \\ 11 & \} \\ 12 & \\ 13 & \\ 14 & \end{tabular} \begin{tabular}{r|c} 1 & import java.util.Arraylist; \\ 2 & \\ 3 & public class InsertAtCommand extends UndoCommand \{ \\ 4 & /l Your field declarations here \\ 5 & \\ 6 & l/ Your constructor code here \\ 7 & \\ 8 & @override \\ 9 & public void execute() \{ \\ 10 & l/ Your code here \\ 11 & \} \\ 12 & \\ 13 & \end{tabular} Current file: GroceryList.java - Load default templateStep 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