Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

http://www.java2s.com/Tutorial/Java/0200__Generics/GenericclassStack.htm 2. Deri

ID: 654912 • Letter: H

Question

http://www.java2s.com/Tutorial/Java/0200__Generics/GenericclassStack.htm

2. Derive input space partitioning tests for the GenericStack class with the following method signatures: public GenericStack (); public void Push (Object X); public Object Pop (); public boolean IsEmt ();

Assume the usual semantics for the stack. Try to keep your partitioning simple, choose a small number of partitions and blocks. (a) De?ne characteristics of inputs (b) Partition the characteristics into blocks (c) De?ne values for the blocks

Explanation / Answer

class Stack<E>
{
private final int size;

private int top;

private E[] elements;

public Stack() {
this(10);
}

public Genric Stack(int s)

{
size = s > 0 ? s : 10;
top = -1;

elements = (E[]) new Object[size];
}

public void push(E pushValue) {
if (top == size - 1) // if stack is full
throw new FullStackException(String.format("Stack is full, cannot push %s", pushValue));

elements[++top] = pushValue;
}

public E pop() {
if (top == -1) // if stack is empty
throw new EmptyStackException("Stack is empty, cannot pop");

return elements[top--]; // remove and return top element of Stack
}
}

class EmptyStackException extends RuntimeException {
public EmptyStackException() {
this("Stack is empty");
}

public EmptyStackException(String exception) {
super(exception);
}
}

class FullStackException extends RuntimeException {
public FullStackException() {
this("Stack is full");
}

public FullStackException(String exception) {
super(exception);
}
}

public class MainClass {

public static void main(String args[]) {
double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
int[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

Stack<Double> doubleStack = new Stack<Double>(5);
Stack<Integer> integerStack = new Stack<Integer>(10);
try {
System.out.println(" Pushing elements onto doubleStack");

for (double element : doubleElements) {
System.out.printf("%.1f ", element);
doubleStack.push(element);
}
} catch (FullStackException fullStackException) {
System.err.println();
fullStackException.printStackTrace();
}

try {
System.out.println(" Popping elements from doubleStack");
double popValue;

while (true) {
popValue = doubleStack.pop();
System.out.printf("%.1f ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.err.println();
emptyStackException.printStackTrace();
}

try {
System.out.println(" Pushing elements onto integerStack");

for (int element : integerElements) {
System.out.printf("%d ", element);
integerStack.push(element);
}
} catch (FullStackException fullStackException) {
System.err.println();
fullStackException.printStackTrace();
}
try {
System.out.println(" Popping elements from integerStack");
int popValue; // store element removed from stack

// remove all elements from Stack
while (true) {
popValue = integerStack.pop();
System.out.printf("%d ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.err.println();
emptyStackException.printStackTrace();
}

}
}