import java.util.Scanner; import java.util.Random; /** Java application whose purpose is to make a simple test of either ** StackViaArray or StackViaLink1, at the choice of the user. */ public class StackTestApp { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { System.out.print("Enter 1 to test StackViaArray, " + "2 to test StackViaLink1:"); int choice = keyboard.nextInt(); Stack stack = null; if (choice == 1) { stack = new StackViaArray(4); } else if (choice == 2) { stack = new StackViaLink1(); } else { System.out.println("Invalid response; program aborting!"); System.exit(1); } performTest(stack, 6, 0.6); System.out.println("Goodbye."); } private static void performTest(Stack stk, int initSize, double deleteProb) { final int maxRand = 20; Random rand = new Random(); for (int i=0; i != initSize; i++) { stk.push(rand.nextInt(maxRand)); } System.out.println("Stack initially: " + stk); while (!stk.isEmpty()) { if (rand.nextDouble() < deleteProb) { int m = stk.pop(); System.out.println("After popping " + m + ": " + stk); } else { int m = rand.nextInt(maxRand); stk.push(m); System.out.println("After pushing " + m + ": " + stk); } } } }