Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions

Question

1. PricewaterhouseCoopers

Answered: 1 week ago

Question

3. SCC Soft Computer

Answered: 1 week ago

Question

2. KMPG LLP

Answered: 1 week ago