Stuck on Java Assignment Involving Stacks

I have a school programming assignment involving stacks that I am completely stuck on for some reason, and I need a good kick in the right direction. I am going to post the complete assignment instructions so that I don't miss anything out, then I'll post my code and where I'm stuck at.

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.

Forget your code for a second, and just think how you would do this by hand.

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

Greenhorn

Posts: 7

posted 3 years ago

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.

Mike. J. Thompson

Bartender

Posts: 689

17

posted 3 years ago

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

Greenhorn

Posts: 7

posted 3 years ago

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.

Mike. J. Thompson

Bartender

Posts: 689

17

posted 3 years ago

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.

Matt Browning

Greenhorn

Posts: 7

posted 3 years ago

Look at my delete method. It has that part. It just doesnt store the change like it should.

Mike. J. Thompson

Bartender

Posts: 689

17

posted 3 years ago

Your delete method implements the delete, but that's not what I said you need to do.

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.

Matt Browning

Greenhorn

Posts: 7

posted 3 years ago

I think I'm finally getting somewhere. For the insert method, it now looks like 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.

Matt Browning

Greenhorn

Posts: 7

posted 3 years ago

I was thinking that I could just create another stack and fill it with objects that contain the attributes for the substrings. This is what I have so far with both of my classes and can go from here (if I'm headed in the right direction) The SmartString class is pre-object idea so it isn't done at all. I should know how to do this, but with personal stuff going on, making it way too hard to think straight.