Question: We would like to modify the ADT Stack interface to have the following new methods: 1) peek2(): T peeks at the entry beneath the top

We would like to modify the ADT Stack interface to have the following new methods:

1)peek2(): T

peeks at the entry beneath the top entry, ie 2nd entry in stack. If the stack has more than 1 entry then peek2 returns the 2nd element in the stack (without removing it) otherwise it returns null.

2)wasta1PopLast(): T

pops the last element in the stack if stack is not empty otherwise it returns null.

3)revenge():

removes the first element in the stack to the last element in the stack.

a) Update the StackInterface accordingly,

b) Update the implementation of the LinkedStackInterface

c) Create a new class Student with the following

* Data: studentName and studentId

* Methods:

i.Constructor with 2 parameters, one for student name and one for student id;

ii.Getter methods getStudentName & getstudentId

iii.toString method

d) Write a test case to test peek2, wasta1PopLast, and revenge(); Create several new students, add them to the stack, then

* Call method testPeek2() and see if it returns the correct result

* Call method wasta1PopLast and see if returns the correct result

* Call method revenge and see if it works correctly

-------------------------------------------------------

/** An interface for the ADT stack. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public interface StackInterface { /** 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

-------------------------------------------------------

import java.util.EmptyStackException; /** A class of stacks whose entries are stored in a chain of nodes. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public final class LinkedStack implements StackInterface { private Node topNode; // References the first node in the chain public LinkedStack() { topNode = null; } // end default constructor public void push(T newEntry) { topNode = new Node(newEntry, topNode); // Node newNode = new Node(newEntry, topNode); // topNode = newNode; } // end push

public T peek() { if (isEmpty()) throw new EmptyStackException(); else return topNode.getData(); } // end peek

public T pop() { T top = peek(); // Might throw EmptyStackException assert (topNode != null); topNode = topNode.getNextNode();

return top; } // end pop

/* // Question 1, Chapter 6: Does not call peek public T pop() { if (isEmpty()) throw new EmptyStackException(); else { assert (topNode != null); top = topNode.getData(); topNode = topNode.getNextNode(); } // end if return top; } // end pop */

public boolean isEmpty() { return topNode == null; } // end isEmpty public void clear() { topNode = null; // Causes deallocation of nodes in the chain } // end clear

private class Node { private T data; // Entry in stack private Node next; // Link to next node

private Node(T dataPortion) { this(dataPortion, null); } // end constructor

private Node(T dataPortion, Node linkPortion) { data = dataPortion; next = linkPortion; } // end constructor

private T getData() { return data; } // end getData

private void setData(T newData) { data = newData; } // end setData

private Node getNextNode() { return next; } // end getNextNode

private void setNextNode(Node nextNode) { next = nextNode; } // end setNextNode } // end Node } // end LinkedStack

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 Databases Questions!