Question: interface StackInterfaceHw 2 . java: / * * return without removing the oldest element that was added to the stack; if stack is empty then

interface StackInterfaceHw2.java:
/**
return without removing the oldest element that was added to the stack; if stack is empty then return null.
Example, If we had push(A); push(C); push(R); push(W); the top of stack would be W, then if we call peekOldest() it would return A and stack remains as is unchanged, ACRW & W is still top of stack.
@return the oldest data entered into the stack, without deleting the data from the stack
*/
public T peekOldest();
/**
swap the most recent element in the stack (top of stack) with the oldest element added to the stack and return True. If stack is empty return false.
Example, if the stack has A,B,C,D and top of stack is D then after calling swap() the stack would have D,B,C,A with A being the top of stack
@return false: stack is empty
true: exchanged top of stack with bottom of stack , so top of stack has
become bottom of stack and bottom of stack has become top of stack
*/
public boolean swap();
/**
This method searches stack for the given object t; if found, true is returned else false is returned.
@param t the object to search for
@return true if t is in the stack otherwise return false
*/
public boolean search(T t);
public interface StackInterfaceHW2{
/**
return without removing the oldest element that was added to the stack; if stack is empty then return null.
Example, If we had push(A); push(C); push(R); push(W); the top of stack would be W, then if we call peekOldest() it would return A and stack remains as is unchanged, ACRW & W is still top of stack.
@return the oldest data entered into the stack, without deleting the data from the stack
*/
public T peekOldest();
/**
swap the most recent element in the stack (top of stack) with the oldest element added to the stack and return True. If stack is empty return false.
Example, if the stack has A,B,C,D and top of stack is D then after calling swap() the stack would have D,B,C,A with A being the top of stack
@return false: stack is empty
true: exchanged top of stack with bottom of stack , so top of stack has
become bottom of stack and bottom of stack has become top of stack
*/
public boolean swap();
/**
This method searches stack for the given object t; if found, true is returned else false is returned.
@param t the object to search for
@return true if t is in the stack otherwise return false
*/
public boolean search(T t);
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);
/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();
/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
/** Removes all entries from this stack. */
public void clear();
}// end StackInterface
A. LinkedStackHW2
What I want you to do for this homework is to modify class LinkedStack and rename it to be LinkedStackHW2.java and have it implement the new interface StackInterfaceHw2.
So LinkedStackHW2 will have everything LinkedStack had before and in addition it will implement the 3 new methods in the interface,
1. public T peekOldest();
2.public boolean swap();
3.public boolean search(T t);
B. Main method in LinkedStackHW2
To test that your methods in class LinkedStackHW2 are implemented correctly, I want you to write several test cases, to test each of the 3 methods; at a minimum, do the following tests and print out their results to the screen.
B1.1 test peekOldest with stack having data
B2.1 test peekOldest with stack empty
B3.1 test for swap
B41 test for search with searchItem in the stack
B51 test for search with searchItem not in the stack

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!