import java.util.Scanner; /** @author R. McCloskey ** @version Sept. 5, 2007 ** ** Java application that tosses a TossableFairCoin object a specified ** number of times and displays the result each time. The number ** of tosses may be provided via a command line argument. ** If no such argument is provided, the user is prompted to enter ** this number. */ public class TossableFairCoinTester { public static void main(String[] args) { String strReps; int numTosses; /* Establish the value of numTosses, either via the command line ** argument (if present) or by prompting the user to key in the value. */ if (args.length > 0) { // command line arg indicates # of coin tosses numTosses = Integer.parseInt(args[0]); } else { // prompt user to enter # of coin tosses System.out.print("Enter number of times to toss the coin:"); Scanner s = new Scanner(System.in); numTosses = s.nextInt(); } /* Create a coin and toss it the prescribed # of times, each time ** reporting the result. */ TossableFairCoin c = new TossableFairCoin(); for (int i=0; i != numTosses; i++) { c.toss(); System.out.println(c.toString()); } // Report the number of times heads and tails were tossed, respectively. System.out.println(); System.out.println("Heads tossed " + c.numHeadsTossed() + " times."); System.out.println("Tails tossed " + c.numTailsTossed() + " times."); assert numTosses == c.numTimesTossed() : "WARNING: Toss count wrong!"; } }