• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SmartString Project using stacks

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am close to being finished with a programming assignment. I just need help on figuring out where I went wrong. I will post the full instructions and the program I have written if anyone can give me the insight on how to fix where I went wrong.
For this assignment, you are to develop a SmartString class that supports inserting into a string, deleting a substring from a string, and an undo method.SmartStringADT.java
Your SmartString class must implement this ADT and therefore implement thefollowing methods:

Note: the last method, toString, should simply return the SmartString and nothing else!
SmartStringDriver.java
A driver class that I’ve provided for you to run quick tests on your SmartString class.
InvalidSmartStringException.java
The Exception that should be thrown should you encounter an invalid Smart String
action such as trying to delete on an empty string.
Program Requirements:
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 JavaAPI Stack is fine or you can use ArrayStack or LinkedStack from the textbook. Consider designing an additional class such as UndoAction, that holds the required information to be placed onto the stack. Only the changes should be stored and not the whole string. Using the example in the introduction, if you add to the Smart String, I am doing great!, then only I am doing great! can be pushed on the stack and NOT the entire Smart String, Hello, how are you doing? I am doing great!

This is what I have so far:









 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Katie, when you use the "Code" button you must first highlight (select) the entire block of code first. You can use the "Review" button to see if it's correct before submitting.
I'll try and fix it for you this time.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your ArrayStack<T> class should not be throwing a SmartStringException. This stack class may or may not have a type T that is a SmartString.

Your stack an undo could be implemented in one of two ways:

The first way is simple but may not meet your requirements. After each SmartString operation, push the new String in its entirety to the stack. This makes undo() very simple in that it just has to pop the string off of the stack. It also makes it easy to set a limit on how big you'll let the stack grow. Most programs that implement multiple levels of undo do this.

The second way is a bit tricky but I believe it is what you're requirements are calling for. You'll need a stack, not of SmartStrings, but of edit operations. When the user requests an edit, e.g. "insert", then an instance of an EditOperation (e.g. InsertOperation) object must be pushed on the stack. This would only contain the minimal amount of information that the user supplied to perform the operation. The InsertOperation could then have an  apply() method that takes a SmartString and returns a new SmartString after the operation has been performed.

So, so far you'd need an abstract class
EditOperation
With an abstract method
SmartString apply( SmartString )

From that, derive a concrete class
InsertOperation
that has as members
string to insert
position to insert
and implements the appropriate apply() method.

So, the stack will then contain instances of classes derived from EditOperation.

To undo then becomes a bit sticky. You'd have to pop the top of the stack and throw it away. Then you'd have to iterate from the bottom of the stack to the new top of the stack, re-apply()ing the edits in order to arrive at the new current SmartString.

Does this make sense?
 
Katie Wonder
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your ArrayStack<T> class should not be throwing a SmartStringException. This stack class may or may not have a type T that is a SmartString.



Do you mean that the peek and pop methods should not throw InvalidSmartStringException?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Katie Wonder wrote:

Carey Brown wrote:Your ArrayStack<T> class should not be throwing a SmartStringException. This stack class may or may not have a type T that is a SmartString.



Do you mean that the peek and pop methods should not throw InvalidSmartStringException?


Nowhere in ArrayStack should you be throwing a xxxSmartStringException.

You have two choices:
  • Have ArrayStack throw an ArrayStackException which you catch in your SmartString code and turn it into an xxxSmartStringException.
  • OR
  • Have your SmartString code test the stack first prior to doing any stack operations and throw an xxxSmartStringException at that point.
  •  
    Katie Wonder
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Before I try and figure out the undo method, I have having a problem on getting the initial smartstring to read out. When it asked me to enter an initial smart string, no matter what I enter the program says that my smart string is nullnullnull... with an index from 0 to 399 and a length of 400.
    What is causing this, and how can I fix it?
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    This is the constructor you are calling. "info" is a local variable, and you are setting it to an empty string, and then throwing away the local variable.
     
    Katie Wonder
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I still cannot figure out what to change the variable to. Should it become temp, or do I need to create a new one?
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Well, your member variables that can hold a string are: string1, str stack, and temp. You shouldn't have temp as a member variable, use local variables for that. So, forget temp. "string1" can hold a string but you are not setting it or using it in your toString() method. "str" is a stack that would have been clearer if you would have named it stringStack. In your  toString() method you call the stack's toString() which will give you the entire stack, not just the top of the stack, which you haven't set yet anyway. So it seems like your "string1" variable should be holding the current string after all the edits have been applied (which is none so far).
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic