Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: stack program

  1. #1
    pinkx is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default stack program

    i want help to build a java program on the following interface
    please its very urgent...

    package jp.co.worksap.intern
    /**
    * The stack class represents a last in first out stack of objects.
    * And so this class can look at the object which has the highest or lowest value
    * So every object on the stack must be comparable to each other
    * @param <E>
    */
    public interface ISortableStack<E extends Comparable<E>> {
    public void push(E e);
    /**
    * Pushes an item oat the top of the stack
    * If the element is null , throws null pointer exception
    */

    public E pop();
    /**
    * Removes an item at the top of the stack stack and returns the object as the value of this function
    * If stack is empty throws EmptyStackException
    * @return
    */

    public E peekMidElement();
    /**
    * Looks at the object which has the middle value among all the objects without removing it from the stack.
    * Returns the object which has the value of order <code>(size()/2)+1</code>
    * <pre>
    * e.g.
    * When the stack has the following values (1,2,5,4,2,6)
    * The method returns 6 and doesn't move the objects
    * </pre>
    * If the stack is empty throws EmptyStackException
    * @return
    * @throws EmptyStackException
    */

    public E peekHighestElement();
    /**
    * Looks at the object which has the highest value among all the objects without removing from the stack.
    * Returns the object which has the value of order <code>size()</code>
    * <pre>
    * e.g.
    * When the stack has the following values (1,2,5,4,2,6)
    * The method returns 6 and doesn't move the objects
    * </pre>
    * If the stack is empty throws EmptyStackException
    * @return
    * @throws EmptyStackException
    */

    public E peekLowestElement();
    /**
    * Looks at the object which has the lowest value among all the objects without removing from the stack.
    * Returns the object which has the value of order <code>1</code>
    * <pre>
    * e.g.
    * When the stack has the following values (1,2,5,4,2,6)
    * The method returns 1 and doesn't move the objects
    * </pre>
    * If the stack is empty throws EmptyStackException
    * @return
    * @throws EmptyStackException
    */

    public int size();

    /**
    * Returns the number of objects in the stack
    * @return
    */

    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    What code do you have so far? Or what problems?
    Copy and paste here any error messages you are having problems with.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Hi pinkx, welcome to the forums.

    You haven't actually asked a question here. As you'll see if you read a few other threads here there is no interest in doing people's homework for them. But there is a lot of help available so that *you* can write this program. I suggest you say what you have done and ask a specific question.

    Specific questions might relate to compiler messages or runtime errors. As for getting started, that's what the comments are there for. Or you might ask about a particular comment if it is unclear.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    Since you already have an interface defined, all you need to do is create your class that you want to use the interface like:

    Java Code:
    public class Example implements ISortableStack{
    
    
    public Example(){
    
    }
    
    //Here is where you need to implement all the methods from your interface.
    
    }
    I think that's the idea.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by pinkx View Post
    public E peekMidElement();
    /**
    * Looks at the object which has the middle value among all the objects without removing it from the stack.
    * Returns the object which has the value of order <code>(size()/2)+1</code>
    * <pre>
    * e.g.
    * When the stack has the following values (1,2,5,4,2,6)
    * The method returns 6 and doesn't move the objects
    ???

    6 is the largest value and it is either the first or last element. In no way is it the mid element.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Looks like somebody's copy and paste got gummed up.

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    stacks are LIFO, so I'm not sure why they want your interface to have that midElement peek. If you have to write this method, I would have a 2nd stack as temporary storage, and pop() from the stack onto that temp stack, get the element you need and push them back.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Why would you need a second Stack? Externally you should not be able to access any element except the top element but internally you can access them all. Assign a temp variable to the top element. Have a loop that iterates "mid" times reassigning temp each time to the next element. Finally return the element that temp is referencing.

  9. #9
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    I'm assuming the original stack needs to be kept intact, thats the idea of a peek.
    Last edited by sehudson; 08-25-2011 at 01:43 AM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    The internal layout of the Stack class data does NOT have to be a "stack". How about if it's an array.
    Finding the middle element in an array is pretty easy.

  11. #11
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    And actually, unless you have someway to get the size of the stack, so that you know what that mid value is, you would have to start off by dumping the stack to a temp stack (or temp string or whatever), get the count, push them back onto the original stack, and then calculate your mid based on that, because the Java stack does not have a size() method, so how would you know what mid is? If you are writing your own stack class, you could put in a size value that you increment everytime someone does a push() or decrement everytime they do a pop(), then consult that value and you could get the mid point, and run your algorithm from there.
    Last edited by sehudson; 08-25-2011 at 01:48 AM.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    The OP is supposed to write a class to implement a stack. Not use Java's Stack class.

  13. #13
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    so yes, put a size variable in your stack, increment it when they do a push(), decrement when they do a pop, and make sure you watch for empty stack exception when they try to do a pop. I would use an arraylist to implement it because a standard array will force you to declare a size when you implement it. You could use an array and make it something ridiculously high, but an ArrayList is better in my opinion.

    I'm reasonably sure that with your peek you need to keep the stack intact, you are just returning the value, not removing it. So calculate your mid based on the size of the stack, pop the elements to a temp stack, grab the value you need, and push everything back on, stack stays intact.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by sehudson View Post
    I'm assuming the original stack needs to be kept intact, thats the idea of a peek.
    Where in my algorithm did I say the element was removed? Remember that elements in the Stack would be Objects and only the reference to the Object would be returned.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    One of the methods that has to be implemented is size().

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    So calculate your mid based on the size of the stack, pop the elements to a temp stack, grab the value you need, and push everything back on, stack stays intact.
    How about if you just used the get method? No pops, no pushes. just do a get.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I hope the actual assignment provides better requirements. From what is posted it is unclear if they can use an array, an ArrayList or completely implement the Stack themselves by also writing a Node class.

  18. #18
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    I see your point.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by sehudson View Post
    but I don't think with the stack that get() should be a method.
    Why not? If it was a private method then it would not be available outside the Stack class. In any given assignment are you rarely limited to only writing the methods mentioned in the specs. You almost always will want to write your own helper methods to achieve your goals.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    So Norm, what would he call on stack to do peekMidElement
    Who is the "he"? If the user of the class, he would call the peekMidElement method.
    with the stack that get() should be a method.
    get is not a method in the stack class that is being implemented.
    peekMidElement is the class's method. We're talking about how to implement that method internal to the class.
    If an array list is used internally, get could be used to find the MidElement.

Page 1 of 2 12 LastLast

Similar Threads

  1. Stack
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 03-04-2011, 09:14 PM
  2. How big is the stack??
    By mgeno216 in forum New To Java
    Replies: 6
    Last Post: 03-04-2011, 09:04 AM
  3. Stack implementation
    By jefrazie in forum New To Java
    Replies: 3
    Last Post: 10-07-2009, 01:59 PM
  4. stack based program
    By hafizul in forum New To Java
    Replies: 5
    Last Post: 03-28-2009, 04:49 PM
  5. Need help please implementing a stack!
    By sfe23 in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 04:19 PM

Posting Permissions

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