Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Need help with a UML diagram involving stacks

    I am very new to Java. I've taken the beginner's course at my local community college and am now in the second part, so my understanding is very rudimentary and I ask your patience.

    Anyway, we are working with stacks which so far are completely blowing my poor little brain away. We were given a UML diagram to work on for homework. I've got the basic skeleton of the class written, but not sure how to write the methods. My professor and our book is no help, so I'm turning to you for assistance. I'm not asking you to write my program for me, just give me a few pointers on where I should go from here. I'll attach a picture of the UML diagram and the code I already have written.

    FYI: He messed up what he wanted the names in the private class Node to be, I wrote in the corrections. And the little scribble to the side was just me being indecisive about where I was going to write the correction and scratching it out

    Need help with a UML diagram involving stacks-uml.jpg

    Java Code:
    import java.util.*;
    
    public class Stack 
    {
       private class Node
       {
          private Node link;
          private int info;
       }
       
       private Node first, newNode, current;
       private int size;
       
       public void push(int i)
       {
       }
       public int peek()
       {
       }
       public int pop()
       {
       }
       public boolean isEmpty()
       {
       }
       public int getSize()
       {
       }
       public void printStack()
       {
       }
    }
    I'm not too worried about the exception handling or the "empty stack" portions, I can't imagine having any trouble with that. Writing the application program shouldn't be a problem either. Again, I'm not looking to have my program written for me, I'm just at a bit of a loss as to where to start. I grasp the basic concepts of stacks, I'm just struggling to write one the way he wants it. Please let me know if I should provide more information, I'll do my best.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    It looks like the stack will internally use a linked list to represent the stack. Do you know what a linked list is?
    It helps when working with linked lists to use paper and pencil. Draw what the linked list looked like before a call to a method and what it should look like after the method executes. Study the steps needed to change the links in the list from the before to the after.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Thank you for your response, yes I do know what a linked list is! We're covering them congruently with stacks. I'm thinking inside of push() is going to have to be
    Java Code:
    newNode = new Node()
    but I'm not positive about that nor am I quite sure what the "i" he wants to be passed in will be used for. I know "first" and "current" are going to be reference variables but I'm not quite sure where to initialize them and in what way.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    Working on the push() method: What will the linked list look like before the push and after the push?

    what the "i" he wants to be passed in will be used for
    Do you understand how a stack is used? Values are pushed onto it and popped off of it. The i contains that value.

    not quite sure where to initialize them and in what way
    Before trying to write any code, determine what those variables will be used for.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Okay, that's the only thing I could think of but I was having a hard time coming up with how to make that work. Something just kinda clicked and I think I figured it out, here's what I have in push() right now:
    Java Code:
    public void push(int i)
       {
          newNode = new Node();
          newNode = first;
          current = first;
          first.info = i;
       }
    It compiles but I know it isn't perfect, at least because I don't think I'll want "first" and "current" always pointing to the same node. I also wasn't sure if push() would have access to "info" because when I tried simply to say "info = i" earlier, it gave me the compile error "cannot find symbol." But it compiles now and I realize why I have to say "first.info" anyway. How does that look so far?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    What values will first, newNode and current hold?

    A suggestion: Change the constructor to the Node class to take the value that it will hold.

    A question: How will the linked list be maintained as values are added to the list (pushed on the stack)?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    1) "first" and "newNode" will always be the last node added to the list, and "current" can be pointed pretty much anywhere. That's my understanding at least. That's how we've discussed using those variables as they relate to linked lists, just kind of in reverse. "first" being the actual first node, I was either planning on having the values be user entered or generated randomly, both handled by the application program. But I hadn't really thought that far ahead yet. And it might not be what you're asking. I'm not sure how I could change the constructor. I assume you mean pass in an argument, but I can't think of how to do that right now. I've been at this for quite some time, I might just need a break.

    2) I guess I'm not sure what you're asking here.

    Again, I really appreciate your help. I'm sorry my understanding is so elementary.

    EDIT: I'm beginning to think it may be best to have "current" and "first" set as the true first and last created nodes, so that I have a reference variable on both ends of the list.
    Last edited by Cash4Gold; 02-28-2014 at 11:33 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    "first" and "newNode" will always be the last node added to the list, and "current" can be pointed pretty much anywhere.
    Sounds like first is the important one. It points to the beginning of the linked list.
    The other two could be local to methods and defined and used as needed.

    It's strange the program specifications via the UML gives names to variables but does NOT define what they are used for.

    2) I guess I'm not sure what you're asking here.
    What is that about?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Quote Originally Posted by Norm View Post
    It's strange the program specifications via the UML gives names to variables but does NOT define what they are used for.
    It is strange, though not surprising given the behavior of my professor.

    And sorry for being vague, I didn't understand what you meant when you asked how the list would be maintained as values are added to the list.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    how the list would be maintained as values are added to the list.
    That is about the linking of nodes as they are pushed on and popped from the stack. Changes need to be made to the variable (first?) that references the first node in the list and to the link variable inside the Node object.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Okay, I understand what you're saying. I need to take a break from this for a while, I'll come back to it and post more later. Thank you, you've been very helpful!

  12. #12
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Hello, I'm back. I've made some progress. I think I've got a pretty much working push() which looks like this:
    Java Code:
    public void push(int i)
       {
          newNode = new Node();
          if (first == null && current == null)
          {
             first = newNode;
             current = first;
             first.info = i;
             current.info = first.info;
          }
          else
          {
             current = newNode;
             current.info = i;
          }  
       }
    peek() is pretty easy, I think. It just returns current.info. isEmpty() is pretty self explanatory as well. But I'm having trouble thinking of how to do pop(), getSize(), and printStack() without using a doubly linked list instead of a singly linked list. Actually, I'm struggling to do them period, but pop() would be easy to do in a doubly linked list. Save the value of "current.info" to a variable (say "x"), set "current=current.prev", return "x". And I think he wants it to be a singly linked list or else he would have listed "prev" and "next" in the UML diagram. For now, I just have pop() returning "current.info" so it's just a clone of peek(). I did that so I could come back to it, but still be able to test the methods I have. But getSize() and printStack() have me stumped. I'll post my failed ideas.
    Java Code:
    public int getSize()
       {
          current = first;
          for (size = 0; current != null; current = current.link)
          {
             size++;
          }
          return size;
          
       }
       public void printStack()
       {
          current = newNode;
          int count = size; 
          if (isEmpty() == true)
             System.out.print("Empty Stack. \n");
          else
          {
             System.out.print(first.info + " ");
             while (current != null)
             {
                System.out.print(first.link.info + " ");
                //count--; not quite sure how to implement counter idea I had
                current = current.link;
             }
          }
    I realized as soon as I tried to test it where my getSize() fails. As soon as I set "current = first" it basically blows away the rest of my list. At least that's what I think. Which would mean having "current = current.link" at the end of the loop basically sets "current" equal to null (again, I think). Also, I don't even know if code really works how it should. I was having trouble in writing my for-loop because I originally said "current.info != null" but got an incomparable types error. So please keep in mind that this code is a completely unfinished idea that may not even be on the right track to begin with.

    As for printStack() I just don't know what to do. Printing an empty stack is no problem, but from there I'm stumped right now. I had some idea to have a running counter that would keep track of my position in the list, but I don't know how to implement it. What you see now is totally half-assed and incomplete, I realize that. It was just the basic foundation for something that probably isn't even a good idea to begin with.

    I'll show you my quick little application program to test my methods. Nothing fancy yet, just wanted to test push() a couple times, as well as peek(), getSize(), and printStack()
    Java Code:
    public class StackTest
    {
       public static void main(String[] args)
       {
          Stack myStack = new Stack();
          
          myStack.printStack();
          myStack.push(5);
          myStack.push(8);
          myStack.push(11);
          myStack.push(55);
          System.out.println("The value of the current node is: " + myStack.peek());
          System.out.println("The size is: " + myStack.getSize());
          myStack.printStack();
          
       }
    }
    Which outputs:
    Empty Stack.
    The value of the current node is: 55
    The size is: 1
    5 Exception in thread "main" java.lang.NullPointerException
    at Stack$Node.access$100(Stack.java:5)
    at Stack.printStack(Stack.java:66)
    at StackTest.main(StackTest.java:14)


    Also any more general bits of advice are appreciated. Thank you!

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    What variable is null? For example what is in newNode when it is assigned to current?

    What is supposed to be in the variable: newNode? What variable points to the first Node in the list?

    To traverse a list, the code would start at the first node and then use its nextNode reference(link) to move to the next node in the list (testing for null before using it).


    Do you have a clear idea what the variables: first, current and newNode are used for?
    newNode's value is ONLY used for a very short span of code when a new Node is created.
    current would be used to chain through the list and has a temporary/local usage for each scan though the list
    first points to the head of the list and is very important
    Last edited by Norm; 03-03-2014 at 09:33 PM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    What variable is null? For example what is in newNode when it is assigned to current?
    I don't think anything except "current.link" at the end of the list is ever null. I guess newNode itself never actually has a value, would that be considered null? Am I just misunderstanding your question?

    What is supposed to be in the variable: newNode? What variable points to the first Node in the list?
    "first" points to the first node in the list, as in the "oldest" if you will. By my understanding, the variable newNode never actually has a value assigned to it. It simply creates a node, we point "current" at that node (unless it's the first node), and assign "current.info" to the value passed in from the test program in "i."

    To traverse a list, the code would start at the first node and then use its nextNode reference(link) to move to the next node in the list (testing for null before using it).
    Okay, that makes sense in the context of moving forward through a list. But since this is a stack, don't I want to print the last value and transverse the list in reverse until I get to the value of "first"? I don't know of a way to do that in a singly linked list.

    EDIT: Having read your edit, I understand what are used for. I'm just having a hard time representing it in the code.
    Last edited by Cash4Gold; 03-03-2014 at 09:44 PM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    Time to go back to the paper and pencil. Draw out a linked list and label it with where the the pointers point.
    Work on all parts of the code to see how the variables/pointers need to be set and changed.
    Work through how to go through the list from the first node to the last node.

    Without a good model in your mind of how to use the variables/pointers, writing code will be a waste of time.

    "first" points to the first node in the list, as in the "oldest"
    That's the opposite of what I've said. I had first point to the youngest. What need is there to have a pointer to the end of the list (oldest)?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Fair enough. You're right, it's definitely time to hit the drawing board again. I have this class tomorrow, hopefully I can get some clarity on some of these issues.

    That's the opposite of what I've said. I had first point to the youngest. What need is there to have a pointer to the end of the list (oldest)?
    I realize that, but I have to write the code in a way that makes sense to me ultimately. "first" being the oldest and "current" being the youngest makes sense. There may be no need to point to the beginning, but then what need is there to have two pointers like that in the first place? He defined two, I have to use them both for something. I'm not trying to be dense or discard your advice, maybe I just really don't understand what the variables should be used for or where the head of the list is. Dumb stacks.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    what the variables should be used for
    my ideas were in post#13
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Okay, I think I figured it out! I finally understand how to utilize each variable correctly as you were suggesting, and I think I've got a working program. Tell me how this looks
    Java Code:
    import java.util.*;
    
    public class Stack 
    {
       private class Node
       {
          private Node link;
          private int info;
       }
       
       private Node first, newNode, current;
       private int size = 0;
       
       public void push(int i)
       {
          newNode = new Node();
          newNode.info = i;
          if (first == null)
          {
             first = newNode;
          }
          else
          {
             newNode.link = first;
             first = newNode;
          }
          size++;  
       }
       public int peek()
       {
          return first.info;
       }
       public int pop()
       {
          int x = first.info;
          first = first.link;
          size--;    
          return x;
       }
       public boolean isEmpty()
       {
          if (first == null && current == null)
             return true;
          else
             return false;
       }
       public int getSize()
       {
          return size; 
       }
       public void printStack()
       {
          current = first;
          if (isEmpty() == true)
             System.out.print("Empty Stack. \n");
          else
          {
             while (current != null)
             {
                System.out.print(current.info + " ");
                current = current.link;
             }
          }
       }
    }
    I figured out a much simpler way to do getSize() and not trying to set "current" until it was time to chain through and print the stack was like a million light bulbs going off in my head. push() is also simplified and doesn't bother with "current" at all, just like it should. Any other pieces of advice? Sorry, it's kinda embarrassing how clueless I was about some of this stuff. I still have to tackle the exception handling and make a test program, but I don't think I'll have any trouble with that.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    Tell me how this looks
    Have you tested it thoroughly? Did it do what you wanted?

    The formatting looks OK, except for some missing {}s with some if statements. If you always use {} then it is less likely you will create problems when adding a statement to an if.

    The value of current shouldn't be used to detect an empty list.
    Last edited by Norm; 03-05-2014 at 07:03 PM.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Have you tested it thoroughly? Did it do what you wanted?
    Yes I have. I created a quick test program that prints an empty stack, runs push() a few times, pops off the last value, prints the size, and then prints the remaining stack. It all seems to work as desired.

    The formatting looks OK, except for some missing {}s with some if statements. If you always use {} then it is less likely you will create problems when adding a statement to an if.
    You're right, I just got lazy since they were single-line if statements. Not a great practice, just chock that one up to laziness. I'll probably go back and fix it. Any other ways I could clean up the formatting a little? I've been using conventions pushed by our professor, but are there any other things I should know to improve it? In a lot of ways I'm just kinda winging it.

    The value of current shouldn't be used to detect an empty list.
    Again, you're right. That was just left over from my previous iterations I hadn't removed. Kinda pointless to do it that way.

Page 1 of 2 12 LastLast

Similar Threads

  1. Assignment involving for and while loops
    By johnWeldon in forum New To Java
    Replies: 1
    Last Post: 03-05-2013, 12:35 AM
  2. error - I think involving my Import Statements
    By Sparky in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 02:31 PM
  3. How to generate Data Flow Diagram(Work Flow Diagram)
    By stsivaraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-22-2010, 11:21 PM
  4. Error involving DecimalFormat
    By kamikaze in forum New To Java
    Replies: 4
    Last Post: 10-18-2010, 02:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •