Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Homework: Point me in the right direction?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework: Point me in the right direction?

    Hey guys!! I am taking Java and I have a programming assignment that I don't understand. We just started talking about stacks so I'm not sure how to even begin, if someone could help. I'll post the assignment instructions below. Thanks in advance!!


    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:

    public interface SmartStringInterface {
    public void insert(int pos, String sstring);
    public void delete(int pos, int count);
    public void undo();
    public String toString();
    }

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework: Point me in the right direction?

    What have you tried?
    What specific questions do you have about your assignment?

    Leave the undo() method until last. Get the others to work first. Then work on a design for the undo(). That will probably require a rewrite of the other methods as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework: Point me in the right direction?

    I'm just completely confused on exactly what I'm supposed to be doing. My previous java teacher really didn't teach us a lot so some of us are behind with this class compared to others. I know what implementing the interface means and stuff, but not sure exactly what the SmartString is supposed to be doing.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework: Point me in the right direction?

    what the SmartString is supposed to be doing.
    The specs talk about the class having methods for inserting Strings and deleting sections. And an undo() method to back out changes.

    It doesn't talk about how to get the current contents of an object.
    IMHO that seems like a hole in the design.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework: Point me in the right direction?

    How does the Stack part work? I think that is throwing a wrench into how I'm understanding it, and I want to make sure I have the right idea.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework: Point me in the right direction?

    That would be for the undo() part. Leave that until later. Get the insert and delete parts working and then come back to the undo.
    As you do the insert and delete, think about what info you need to save to be able to undo them.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework: Point me in the right direction?

    What would the count be referring to? I understand the concepts of push and pop, but not sure what the point of this program is to do. I think if I can understand it, I might be able to do it even with the stack.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework: Point me in the right direction?

    the point of this program
    I'd say the purpose is to make changes and undo the changes
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework: Point me in the right direction?

    This is what I got so far for SmartString. I know we used "top" in the examples for class, but I'm not sure if i have the stack correct or not. I need a shove in the right direction...

    Edit: I was wondering after this if I should use String Builder instead of String.

     
    public class SmartString implements SmartStringInterface
    {
    	private int stack [];
    	private int top;
     
     
    	public void insert(int pos, String sstring)
    	{
     
    	}
     
    	public void delete(int pos, int count)
    	{
     
    	}
     
     
    	public void undo()
    	{
     
    	}
     
     
    	public String toString()
    	{
    		String s = "";
        	for (int i = 0; i < stack.length; i++)
           {
        	   s += stack[i];
           }
     
        	return s;
    	}
    }
    Last edited by mattmattmatt; May 29th, 2014 at 09:19 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework: Point me in the right direction?

    How does the class hold the characters in the SmartString?
    That is what the the toString() method should return, not what is in the stack.

    I'm not sure if i have the stack correct or not
    The stack is for the undo() method. Do that later after the insert and delete methods are working.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework: Point me in the right direction?

    Any way to give me an example of this? I know how the push and pop work and how to set those up. Is insert and delete the same as those 2? If so, where do the count and pos come from? I know they will be the values taken in by those methods, but not sure what they're supposed to represent and where you get them from.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework: Point me in the right direction?

    I suggest you do the insert() and delete() logic first. They will not use a stack. When they are done, then work on the undo() method which will use the stack.
    Example of insert: Given a SS: "asd" inserting "xy" at 1 could give: "asxyd"
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework: Point me in the right direction?

    For insert and delete, is the easiest way to do it is StringBuilder?

    --- Update ---

    Here are my insert and delete methods. I commented the changes part out since that is how I was storing the new string in the stack, but it's wrong. So the deleting and inserting themselves should be correct since my string is string, the substring I'm inserting is sstring (which I hard code into the driver) and pos is the position in the string that the substring is inserted or deleted (which I also hard code when I list the sub strings)

    public void insert(int pos, String sstring)
    	{
    		string.insert(pos, sstring);
    		//changes.push(string.toString());
    	}
     
    	public void delete(int pos, int count) 
    	{
    		string.delete(pos, count);
    		//changes.push(string.toString());
    	}

Similar Threads

  1. Replies: 1
    Last Post: April 6th, 2014, 03:14 AM
  2. Can Anyone Point me in the right direction?
    By ImyMTD in forum AWT / Java Swing
    Replies: 8
    Last Post: May 4th, 2013, 02:29 AM
  3. Can anyone point me in the right direction dealing with bytes
    By derekxec in forum Java Theory & Questions
    Replies: 5
    Last Post: August 18th, 2012, 01:44 PM
  4. HI WORLD! Can anyone point a complete novice in the right direction??
    By Irish-novice in forum Member Introductions
    Replies: 4
    Last Post: June 26th, 2012, 05:16 PM
  5. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM