Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a stack ADT using array Recall the three aspect of an ADT as show in the diagram. You are going to play a role

Implement a stack ADT using array Recall the three aspect of an ADT as show in the diagram. You are going to play a role as an ADT programmer to implement a stack ADT using array. The interface has been provided as Stack.java. Given this interface, the application programmer has developed a simple application TestStack.java. Your task for today is to implement the operations/methods in the ADT, which are hidden to application programmers. A backbone of the ADT is provided in BasicStack.java and necessary code is added to compile it successfully. You will need to replace your implementation in each of the methods. Application ADT Interface ADT Implementation Uses ADT operations Only sees interface Provides functionality Protects data by hiding it Hides detail testStack.java Stack.java BasicStack.java After the implementation is finished, try another application CardStackApp.java that uses the stack ADT, where each element in the stack is a string of poker card. Observe how the generic element t /** * Defines the interface to a stack collection. * A collection of objects that are inserted and removed according to the last-in first-out principle. * */ public interface Stack { /** Pre: Post: the stack is unchanged. * Return: the number of elements in this stack. */ public int size();

/** Pre: Post: the stack is unchanged. * Return: true if this stack contains no elements. */ public boolean isEmpty(); /** Pre: Post: adds the specified element to the top of this stack. Return: void */ public void push(E item);

/** Pre: Post: removes the top element from this stack. Return: the top element from this stack. */ public E pop();

/** Pre: Post: the stack is unchanged. Return: the top element from this stack. */ public E top();

/** * Check if an element is found in the stack Pre: Post: the stack is unchanged. Return: true if the item is found in the stack; false otherwise */ public boolean contains (E item); /** * grab each by doing a pop. It's a destructing operation * if the element is not in the stack, throw an exception Pre: Post: every item above the item including item itself is popped out from the stack Return: the element item from this stack. */ public E access(E item); }

/** This is a Stack ADT implemented using an array */ public class BasicStack implements Stack { // instance variables private E [] data; // data is generically typed private int stackPointer; // the point of the stack pointer is to simply increment/decrement based on the size of the stack // class constructor public BasicStack(){ data = (E[]) new Object[1000]; stackPointer = 0; } public BasicStack(int capacity){

} /** Pre: Post: the stack is unchanged. * Return: the number of elements in this stack. */ public int size() { // student add content to this method return 1; } /** Pre: Post: the stack is unchanged. * Return: true if this stack contains no elements. */ public boolean isEmpty(){ // student add content to this method return true; } /** Pre: Post: adds the specified element to the top of this stack. Return: void */ // special case: push when the stack is full public void push (E newItem){ // student add content to this method }

/** Pre: Post: removes the top element from this stack. Return: the top element from this stack. */ // special case: pop when the stack pointer is currently 0 and we try to pop something off of it public E pop(){ // student add content to this method E item; return item; }

/** Pre: Post: the stack is unchanged. Return: the top element from this stack. */ public E top(){ // student add content to this method E item; return item; }

/** * Check if an element is found in the stack Pre: Post: the stack is unchanged. Return: true if the item is found in the stack; false otherwise */ public boolean contains (E item){ return true; } /** * grab each by doing a pop. It's a destructing operation Pre: Post: every item above the item including item itself is popped out from the stack Return: the element item from this stack. */ // special case: if the element is not in the stack, throw an exception public E access(E item){ // student add content to this method E item; return item; }

}

public class TestStack { public static void main(String[] args) { Stack S = new BasicStack<> (); S.push(7); S.push(5); if (S.isEmpty()) System.out.println("The stack is empty"); else System.out.println("The stack is not empty"); S.push(10); S.push(18); S.push(12); System.out.println("Current size of the stack:" + S.size()); System.out.println("Element that is popped from the stack: " + S.pop()); System.out.println("Current size of the stack: " + S.size()); System.out.println("The top element on the stack: " + S.top()); System.out.println("Element that is popped from the stack: " + S.pop()); System.out.println("Grab element from the stack: " + S.access(5)); System.out.println("Current size of the stack:" + S.size()); S.push(20); System.out.println("Current size of the stack:" + S.size()); System.out.println("Poped stack element: " + S.pop()); System.out.println("Poped stack element: " + S.pop()); System.out.println("Poped stack element: " + S.pop()); } }

/** * A application that uses the Stack ADT via the Stack interface * */ public class CardStackApp { Stack stack = new BasicStack<>();

public static void main(String[] args) { CardStackApp app = new CardStackApp(); app.stackCards(); app.unstackCards(); //restack cards app.stackCards(); //how many cards are on the deck app.desckSize(); //do we have queen of hearts in the deck app.containsCard("Queen of Hearts");

//do we have a joker app.containsCard("Joker");

//go to the king of diamonds app.goToCard("King of Diamonds");

//now how many cards are on the deck app.desckSize(); }

public void stackCards() { //stack the spade suit stack.push("Ace of Spades"); stack.push("2 of Spades"); stack.push("3 of Spades"); stack.push("4 of Spades"); stack.push("5 of Spades"); stack.push("6 of Spades"); stack.push("7 of Spades"); stack.push("8 of Spades"); stack.push("9 of Spades"); stack.push("10 of Spades"); stack.push("Jack of Spades"); stack.push("Queen of Spades"); stack.push("King of Spades");

//stack the diamond suit stack.push("Ace of Diamonds"); stack.push("2 of Diamonds"); stack.push("3 of Diamonds"); stack.push("4 of Diamonds"); stack.push("5 of Diamonds"); stack.push("6 of Diamonds"); stack.push("7 of Diamonds"); stack.push("8 of Diamonds"); stack.push("9 of Diamonds"); stack.push("10 of Diamonds"); stack.push("Jack of Diamonds"); stack.push("Queen of Diamonds"); stack.push("King of Diamonds");

//stack the club suit stack.push("Ace of Clubs"); stack.push("2 of Clubs"); stack.push("3 of Clubs"); stack.push("4 of Clubs"); stack.push("5 of Clubs"); stack.push("6 of Clubs"); stack.push("7 of Clubs"); stack.push("8 of Clubs"); stack.push("9 of Clubs"); stack.push("10 of Clubs"); stack.push("Jack of Clubs"); stack.push("Queen of Clubs"); stack.push("King of Clubs");

//stack the heart suit stack.push("Ace of Hearts"); stack.push("2 of Hearts"); stack.push("3 of Hearts"); stack.push("4 of Hearts"); stack.push("5 of Hearts"); stack.push("6 of Hearts"); stack.push("7 of Hearts"); stack.push("8 of Hearts"); stack.push("9 of Hearts"); stack.push("10 of Hearts"); stack.push("Jack of Hearts"); stack.push("Queen of Hearts"); stack.push("King of Hearts"); } public void unstackCards() { //now pull the cards off the stack and print them while(stack.size() > 0) { System.out.println(stack.pop()); } }

public void containsCard(String card) { System.out.println(stack.contains(card)); }

public void goToCard(String card) { System.out.println(stack.access(card)); }

public void desckSize() { System.out.println(stack.size()); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

=+ Is the information documented and verifiable?

Answered: 1 week ago

Question

Find the derivative of y= cos cos (x + 2x)

Answered: 1 week ago

Question

Use service tiering to manage the customer base and build loyalty.

Answered: 1 week ago