Question: You are to create a class called Stack which works just like the built-in java.util.Stack. It will have 5 methods: push, pop, peek, empty, and

You are to create a class called Stack which works just like the built-in java.util.Stack. It will have 5 methods: push, pop, peek, empty, and search.

(Make sure it mathes the tester).

Tester:

import java.util.*;

public class StackTester

{

public static void main(String[ ] args)

{

//get the arguments for the test

java.util.Scanner kb = new java.util.Scanner(System.in);

String test = kb.nextLine();

//***************************************************

//***************************************************

if (test.equalsIgnoreCase("1. testing push and pop"))

{

boolean printDescription = true;

boolean checkChanges = true;

try

{

int[ ] theArgs = { 2, 7, -1, 0, 2 };

Stack myStack = new Stack();

System.out.println(" Starting with an empty Stack");

System.out.println("(If you create a Stack, you can push an int like 2 into it.");

System.out.println("Java will automatically \"wrap\" 2 into an instance of the Integer class");

System.out.println("when it is pushed and then \"unwrap\" it back into an int 2 when it is popped). ");

System.out.println(" -----pushing " + theArgs.length + " elements");

for (int i=0; i

{

System.out.print("calling push(" + theArgs[i] + ")");

int result = myStack.push(theArgs[i]);

System.out.println("returned " + result);

}

System.out.println(" -----Now popping the stack until can't pop anymore (to see contents)");

while(true)

{

System.out.print("-->calling pop() ");

int result = myStack.pop();

System.out.println("returned " + result);

}

}

catch (Throwable ex)

{

//System.out.println("\t" + ex);

System.out.println(ex.getClass().getName());

}

}

//***************************************************

//***************************************************

else if (test.equalsIgnoreCase("2. testing peek method, starting with empty Stack and then pushing elements one at a time, testing peek() each time"))

{

boolean printDescription = true;

boolean checkChanges = true;

String[ ] theArgs = { "AAA", "BBB", "CCC", "DDD", "EEE" };

Stack myStack = new Stack();

System.out.println(" Starting with an empty Stack ");

try

{

String result;

System.out.print("-->calling peek() ");

result = myStack.peek();

System.out.println("returned " + result);

}

catch(Throwable ex)

{

System.out.println(ex.getClass().getName());

}

for (int i=0; i

{

try

{

System.out.println("now telling it to push(" + theArgs[i] + ")");

myStack.push(theArgs[i]);

String result;

System.out.print("-->calling peek() ");

result = myStack.peek();

System.out.println("returned " + result);

}

catch (Throwable ex)

{

//System.out.println("\t" + ex);

System.out.println(" \t" + ex.getClass().getName());

}

}

}

//***************************************************

//***************************************************

else if (test.equalsIgnoreCase("3. testing empty() method, starting with empty Stack and then pushing elements one at a time, testing empty() each time"))

{

boolean printDescription = true;

boolean checkChanges = true;

double[ ] theArgs = { 2.5, 7.99, -1.02 };

Stack myStack = new Stack();

System.out.println(" Starting with an empty Stack");

System.out.println("(If you create a Stack, you can push a double like 2.5 into it.");

System.out.println("Java will automatically \"wrap\" 2.5 into an instance of the Double class");

System.out.println("when it is pushed and then \"unwrap\" it back into a double 2.5 when it is popped). ");

try

{

boolean result;

result = myStack.empty();

System.out.print("-->calling empty() ");

System.out.println("returned " + result);

}

catch (Throwable ex)

{

//System.out.println("\t" + ex);

System.out.println(" \t" + ex.getClass().getName());

}

for (int i=0; i

{

try

{

System.out.println("now telling it to push(" + theArgs[i] + ")");

myStack.push(theArgs[i]);

boolean result;

result = myStack.empty();

System.out.print("-->calling empty() ");

System.out.println("returned " + result);

}

catch (Throwable ex)

{

//System.out.println("\t" + ex);

System.out.println(" \t" + ex.getClass().getName());

}

}

}

//***************************************************

//***************************************************

else if (test.equalsIgnoreCase("4. testing search method"))

{

boolean printDescription = true;

boolean checkChanges = true;

int[ ] theArgs = { 2, 7, -1, 0, 2, 5 };

Stack myStack = new Stack();

System.out.println(" Starting with an empty Stack ");

try

{

int result;

result = myStack.search(7);

System.out.println("-->search(7) returns: " + myStack.search(7));

System.out.println();

for (int i=0; i

{

System.out.println("now telling it to push(" + theArgs[i] + ")");

myStack.push(theArgs[i]);

}

System.out.println();

for (int i=0; i

{

result = myStack.search(theArgs[i]);

System.out.println("-->search(" + theArgs[i] + ") returns " + result);

}

}

catch (Throwable ex)

{

//System.out.println("\t" + ex);

System.out.println(" \t" + ex.getClass().getName());

}

}

}

}

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!