import java.util.Scanner; import java.util.Random; /** Java application whose purpose is to make a simple test of either ** QueueViaArray or QueueViaLink1, at the choice of the user. */ public class QueueTestApp { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { System.out.print("Enter 1 to test QueueViaArray, " + "2 to test QueueViaLink1:"); int choice = keyboard.nextInt(); Queue queue = null; if (choice == 1) { queue = new QueueViaArray(3); } else if (choice == 2) { queue = new QueueViaLink1(); } else { System.out.println("Invalid response; program aborting!"); System.exit(1); } performTest(queue, 12, 0.6); System.out.println("Goodbye."); } private static void performTest(Queue q, int initSize, double deleteProb) { final int maxRand = 20; Random rand = new Random(); for (int i=0; i != initSize; i++) { q.enqueue(rand.nextInt(maxRand)); } System.out.println("Queue initially: " + q); while (!q.isEmpty()) { if (rand.nextDouble() < deleteProb) { int m = q.dequeue(); System.out.println("After dequeueing " + m + ": " + q); } else { int m = rand.nextInt(maxRand); q.enqueue(m); System.out.println("After enqueueing " + m + ": " + q); } } } }