Tuesday, September 13, 2011

Generic Java Stack - Work application Question

Question :
package jp.co.worksap.intern;
    /**
    *The Stack class represents a last-in-first-out(LIFO) stack of objects
    *And this class can look at the object which has the highest (lowest or so) value.
    *So every object onto the stack must be comparable to each other.
    *@param<E.
    */
    public interface ISortableStack<E extends Comparable<E>>{
    /**
    *Pushes an item onto the top of this stack.
    *If the Element e is null, throws NullPointerException.
    * @param e
    *@throws NullPointerException
    */
    public void push(E e);
    /**
    *Removes the object at the top of this stack and returns that object as the value of this function.
    *If the stack is empty, throws EmptyStackException.
    *@return
    *@throws EmptyStackException
    */
    public E pop();
    /**
    *Locks at the object which has the middle value among the all objects without removing it from the stack.
    *Returns the object which has the value of following order <code>(size()/2)+1</code>
    *<pre>
    *e.g. When the stack has the following values (1, 2, 5, 4, 2, 6)
    *this method returns 4 and doesn't remove the object.
    *</pre>
    *If the stack is empty, throws EmptyStackException.
    *@return
    *@throws EmptyStackException
    */
    public E peekMidElement();
    /**
    *Looks at the object which has the highest value among the all objects without removing it from the stack.
    *Returns the object which has the value of the following order <code>size()</code>
    *<pre>
    *e.g. When the stack has the following values (1,2,5,4,2,6)
    *this method returns 6 and doesn't remove that object
    *</pre>
    *If the stack is empty, throws EmptyStackException.
    *@return
    *@throws EmptyStackException
    */
    public E peekHighestElement();
    /**
    *Looks at the object which has the lowest value among the all objects without removing it from the stack.
    *Returns the object which has the value of the following order <code>1</code>
    *<pre>
    *e.g. When the stack has the following values (1,2,5,4,2,6)
    *this method returns 1 and doesn't remove the object.
    *</pre>
    *If the stack is empty, throws EmptyStackException.
    *@return
    *@throws EmptyStackException
    */
    public E peekLowestElement();
    /**
    *Returns the number of objects in this stack.
    *@return
    */
    public int size();

Answer :
Mediafire Links:
Method 1
Method 2