Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package testMax; import static org.junit.Assert.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import stackPackage.*;//AJF public class Test_max { LinkedComparableStack stk1; @Before public void setUp() throws Exception

image text in transcribed

package testMax;

import static org.junit.Assert.*;

import org.junit.Assert; import org.junit.Before; import org.junit.Test;

import stackPackage.*;//AJF

public class Test_max {

LinkedComparableStack stk1; @Before public void setUp() throws Exception { stk1 = new LinkedComparableStack(); }

@Test(expected = StackUnderflowException.class) //AJF public void test_findMax_on_an_emptyStack() { stk1.findMax(); }

}

********************************

package stackPackage;

public class LinkedComparableStackextends Comparable> extends LinkedStack {

public LinkedComparableStack() {

// constructor uses superclass constructor method

super();

}

/* AJF:

* In this class, if A and B are both type T,

* then we can use A.compareTo(B) for comparing A to B.

* The result will be 0 if A is the same as B,

* positive if A is greater than B,

* and negative if A is less than B.

* (So, A.compareTo(B) basically does A-B.)

*

* Note the "extends Comparable" bit above, which makes

* sure generic type T is a comparable type.

*

*/

/*

* AJF: I recommend you use findMax() as a public wrapper

* which calls a private *recursive* function myPrivateFindMax

* as indicated below.

*/

public T findMax() throws StackUnderflowException{

}

/*

* AJF: The reason myPrivateFindMax is private, is because

* the intention is for it to be called with top as the

* initial parameter, and top is protected.

*/

private T myPrivateFindMax(LLNode current) {

}

}

***********************

package stackPackage;

public class LinkedStack implements UnboundedStackInterface {

protected LLNode top; // reference to the top of this stack

public LinkedStack() {

top = null;

}

public void push(T element)

// Places element at the top of this stack.

{

LLNode newNode = new LLNode(element);

newNode.setLink(top);

top = newNode;

}

public void pop()

// Throws StackUnderflowException if this stack is empty,

// otherwise removes top element from this stack.

{

if (!isEmpty()) {

top = top.getLink();

} else

throw new StackUnderflowException("Pop attempted on an empty stack.");

}

public T top()

// Throws StackUnderflowException if this stack is empty,

// otherwise returns top element from this stack.

{

if (!isEmpty())

return top.getInfo();/* LinkedStack stk;

@Before

public void setUp() throws Exception {

stk = new LinkedStack();

}

*/

else

throw new StackUnderflowException("Top attempted on an empty stack.");

}

public boolean isEmpty()

// Returns true if this stack is empty, otherwise returns false.

{

if (top == null)

return true;

else

return false;

}

//AJF

private String printFromBottom(LLNode current)

// recursively add each string from bottom of stack

{

String stringSoFar = "";

if (current.getLink() != null) {

// more links to explore

stringSoFar += printFromBottom(current.getLink());

}

stringSoFar += current.getInfo().toString();

stringSoFar += "|";

return(stringSoFar);

}

//AJF

public String toString()

// returns a string containing contents of the stack from bottom to top.

{

if (isEmpty()) {

return "Empty Stack";

}

String returnString = "";

returnString += "bottom|";

returnString += printFromBottom(top);

returnString += "top";

return returnString;

}

public int sizeIs() {

LLNode current = top;

int size = 0;

if (current == null) {

return 0;

}

size++;

while (current.getLink() != null) {

size++;

current = current.getLink();

}

return size;

}

public void clear() {

top = null;

}

}

2. Part B Download the file Lab04-FindMax-Source.zip to a directory available to Eclipse, and import it into Eclipse as usual. 3. Open the file Test_max.java and run it as a JUnit Test. You should get a red bar and errors. (a) In Test max.java, write more tests to test the method findMax ), which takes a LinkedComparableStack object and finds the maximum item in the stack. Note that the LinkedComparableStack class extends the LinkedStack class, so you can use pushO to put integers onto the stack stk1. These tests should all fail initially (b) Look at LinkedComparableStack. java. Read my notes about the class in that file. Com- plete the methods findMax) and myPrivateFindMax) as described in the ile's com- ments. Note that both return a reference of type T. The return value from the findMax() method should point to the largest value in the stack. 4. Answer the following questions is the file answers.txt: (a) Since we are testing findMax() with integers, why don't we use the > operator to test if we've found a bigger integer? (b) Can you think why we didn't add findMax() to the interfaces in this package, or even to the LinkedStack class? 5, Take a screenshot of the entire Eclipse window showing each test file with a green JUnit bar after the last task is complete. Call them lab04AScreenshot png and lab04BScreenshot. 2. Part B Download the file Lab04-FindMax-Source.zip to a directory available to Eclipse, and import it into Eclipse as usual. 3. Open the file Test_max.java and run it as a JUnit Test. You should get a red bar and errors. (a) In Test max.java, write more tests to test the method findMax ), which takes a LinkedComparableStack object and finds the maximum item in the stack. Note that the LinkedComparableStack class extends the LinkedStack class, so you can use pushO to put integers onto the stack stk1. These tests should all fail initially (b) Look at LinkedComparableStack. java. Read my notes about the class in that file. Com- plete the methods findMax) and myPrivateFindMax) as described in the ile's com- ments. Note that both return a reference of type T. The return value from the findMax() method should point to the largest value in the stack. 4. Answer the following questions is the file answers.txt: (a) Since we are testing findMax() with integers, why don't we use the > operator to test if we've found a bigger integer? (b) Can you think why we didn't add findMax() to the interfaces in this package, or even to the LinkedStack class? 5, Take a screenshot of the entire Eclipse window showing each test file with a green JUnit bar after the last task is complete. Call them lab04AScreenshot png and lab04BScreenshot

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

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions