Instructions: For this assignment, you are to develop a SmartString class that supports insert into a string, delete a substring from a string, and an undo method. Your SmartString class must conform to (implement) the following interface:
You must use a Stack to store the operations in order to support undo operations. Note that multiple undo operations can be performed in sequence. Using the Java API Stack is fine or you can use ArrayStack from the textbook. Design a class to hold the required information to be placed onto the stack. Only the changes should be stored and not the whole string. you should write a driver program to instantiate a SmartString and thoroughly test your methods. A portion of your grade will be based on how well you test your SmartString class. You should handle stack underflow, but you can assume that the driver will not have any Index out of Bounds errors.
Here is my code, including the files she gave us to use the various methods. My file will be at the bottom (ArrayStack)
**Note: I have not done anything with the driver and the code is blank for it, so I didn't paste that as I was going to do that after I got the methods done.**
The Issue I'm having is with the Undo() method. As the instructions state, she only wants the changes stored. The way I have it currently is that it stores the "new" string after every change and then can use pop to revert the most recent change and have the string prior to the change as a result. The professor said that was incorrect, so I am stuck and have no idea where to start.
If your current string is set to This is a String, and then I call
1) What is the opposite of what I just did? (Hint: you already have an operation that will do that).
2) What information do you need to store in order to perform that operation?
Matt Browning wrote:Yeah the actual inserting and deleting I think I get...Its how to store the CHANGES that I am stuck on. I am not sure how to store them to the stack and be able to undo some of those changes.
If you want to undo an operation you need to identify the inverse of the operation. That was the point of the question I asked you. What is the inverse of inserting? What is the inverse of deleting? What information do you need for both of those things?
You're going to need to write a class that to be stored on the stack encapsulating the undo operation. Every time someone performs an operation on the string you are going to need to gather all the information you need for the inverse of that operation, create a new instance of the class holding that information, and then put it on the stack.
Matt Browning wrote:I kmow the undo of an insert is a delete and the undo 8f a delete is an insert, but I dont know to code the insert delete or undo once you throw the stack in. I know what the idea is and what it is supposed to do, but no idea how to actually code it other than the way I currently have it.
Ok, well lets break it up into steps. Forget about the stack for a minute.
Can you write a class that will perform the inverse of an insert? You know that the inverse of an insert is a delete. Look at the delete() method and see what information it needs when you call it. Try and create a class that will undo a specific delete. It will need a reference to the SmartString, and it will need to know what it has to delete.
Can you write a class that will call the delete method, and pass appropriate values to delete() so that it performs the inverse of a particular insert() operation.
This class will be what you put on the stack whenever an insert is performed.
The class will need to store appropriate information so that it can do this.
For delete and undo, I'm still not sure, especially undo. For delete I think I can use StringBuilder and have it build a substring starting with the position entered and going through the count and storing that substring into the changes stack like insert does.
Undo is where it gets hairy. I'll have the substrings from insert and delete, but it won't know whether to insert or delete. I know it will recall the most recent change, so it will be in the right order, but not sure how to code it to know whether to insert or delete.
1) Don't have multiple stacks. It will be difficult to know which stack to pop.
2) The Objects class is a step in the right direction, but it needs a better name. Also, get rid of all the accessors. There are other improvements that can be made, but we can look at those later.
3) You never use the Objects class anywhere, why not? You need to create it and put it on the Stack.
4) Your Objects class doesn't undo anything. It needs to reverse the insert or delete that was performed on the smart string.