Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java, write a MyStack class that implements the StackInterface using a linked list. Your MyStack class is required to use a private inner class

Using Java, write a MyStack class that implements the StackInterface using a linked list. Your MyStack class is required to use a private inner class implementation for the node of the linked list called Node. In addition to implementing the methods defined in StackInterface, your MyStack class must override the toString method defined in the Object class. toString should return the String representation of the current state of MyStack. For example, if we created a new MyStack object and pushed 256, 1, 9, 20, and 2018 onto the stack, the toString method should return: TOP: 2018 | 20 | 9 | 1 | 256 |

Attached is the Stack Interface and MyStack tester for reference. Thanks!image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

An interface for the ADT stack @author Frank M. Carrano @author Timothy M. Henry @version 4.0 public interface StackIntertace Adds a new entry to the top of this stack @param newEntryAn object to be added to the stack. */ public void push(T newEntry); Removes and returns this stack's top entry. @returnThe object at the top of the stack. @throws if the stack is empty before the operation. */ public T pop(); /**Retrieves this stack's top entry @returnThe object at the top of the stack. @throwsEmptyStackExceptign if the stack is empty. */ public T peek(); /**Detects whether this stack is empty. public boolean isEmpty(); @ret if the stack is empty. */ Removes all entries from this stack. */ public void clear(); } // end StackInterface import static org.junit.Assert. import javalang.reflect.Modifier import java.uti1. EnptyStackException; import org.junit.Before; import org.junit.FixMethodorder; import org.junit.Test; import org.junit.runners.MethadSorters; SFixMethodorder(MethodSorters.NAME_ASCENDING) public class MyStackTest t MyStackestring> stack; private String n1,n2,n3; throws Java.lang.Exception sBefore public void setup) throws Exception i stacknew Mystack) ni"Able"; n2"Baker; n3"Charlie" private void pushAl1Elements)t stack.push(n1); stack push n2) stack-push(n3) SSuppressWornings("rawtypes) STest public void MyStackNestedClassTest) i MyStack testMyStack- new MyStack string> Class -testMyStack.getclass() Class] allNested-c.getDeclaredclasses); assertEquals( "You must have only one nested class in your MyStack class", 1, allNested.length); Class nestedallNested[8] assertEquals( "For the name of MyStack's inner class we", Node", nested.getme.split1) *Test push anto an empty stack STest public final void testPush1) stack push "Radney Ram") assertEquals (stack.peek, "Rodney Ram" *Test push onto a stack with elenents STest public final void testPush2) t pushAllElenents); stack push "Radney Ram") assertEquals (stack.peek, "Rodney Ram" pushAllElenents); stack pop) assertEquals("Baker, stack.peek)) *Test peak after clear STest (expected-EnptyStackException.class) public final void testPeek4) pushAllElenents) stack.clear); stack.peek) *Test method for isEmpty initially STest public final void testIsEnpty1) assertTrue stack.isEmpty)) *Test method for isEmpty after push STest public final void testIsEpty2) pushAllElenents) assertFalse(stack.isEmpty)) *Test method for isEmpty after push & pap STest public final void testIsEpty3) pushAllElenents) stack pop) assertFalse(stack. sEmpty( )) ; *Test method for isEmpty after push & paps STest public final void testIsEpty4) pushAllElenents); stack pop) stack.pop): stack.pop): assertTrue (Stack. isEmpty()); *Test method for isEmpty after clear STest public final void testIsEptys) pushAllElenents) stack.clear); assertTrue stack.isEmpty) *Test method for clear() after initialization gTest public final void testclearl) 1 *Test push onto a ful1 stack STest public final void testPush3) pushAllElenents) stack push Alan Turing") stack pushAda Lovelace"; stack push "Adm. Grace Hopper) Test pushing a null elenent STest public final void testPush4) pushAllElenents) stack push(null); assanttquas(null, stack.peek)) Test method for pop) an empty stack STest (expected-EnptyStackException.class) public final void testPopi) stack-pop *Test method for pop) after push STest public final void testPop20 i pushAllElenents) assertEquals( "Charlie, stack.pop)) *Test method for pop) after clear() STest (expected-EnptyStackException.class) public final void testPop3) 1 pushAllElenents) stack.clear) stack.pop(): *Test peek() before pushing anything STest (expected-EnptyStackException.class) public final void testPeeki) stack.peek); Test peek() after push STest public final void testPeek2) pushAllElenents); assertEquals("Charie, stack.peek)) Test peek() after push& pop STest public final void testPeek3) stack.clear) assertTrue(stack.isEmpty)) Test method for clear) after pushing STest publse final voia testclearac) ( pushAllenents) stack.clear); assertTrue(stack.1sEmpty))

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

More Books

Students also viewed these Databases questions

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago

Question

Why could the Robert Bosch approach make sense to the company?

Answered: 1 week ago