Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 1 Chapter 6 Confirmation Dialog Formatting Output.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 1 Chapter 6 Confirmation Dialog Formatting Output."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 1 Chapter 6 Confirmation Dialog Formatting Output Random Numbers Timing Programs Input Validation with Scanner

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 2 Confirmation Dialog A confirmation dialog can be used to prompt the user to determine whether to continue a repetition or not. JOptionPane.showConfirmDialog(null, /*prompt*/ "Play Another Game?", /*dialog title*/ "Confirmation", /*button options*/ JOptionPane.YES_NO_OPTION);

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 3 Example: Confirmation Dialog boolean keepPlaying = true; int selection; while (keepPlaying){ //code to play one game comes here //... selection = JOptionPane.showConfirmDialog(null, "Play Another Game?", "Confirmation", JOptionPane.YES_NO_OPTION); keepPlaying = (selection == JOptionPane.YES_OPTION); }

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 4 Formatting Output We call the space occupied by an output value the field. The number of characters allocated to a field is the field width. The diagram shows the field width of 6. From Java 5.0, we can use the Formatter class. System.out (PrintStream) also includes the format method.

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 5 The Formatter Class We use the Formatter class to format the output. First we create an instance of the class Formatter formatter = new Formatter(System.out); Then we call its format method int num = 467; formatter.format("%6d", num); This will output the value with the field width of 6.

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 6 The format Method of Formatter The general syntax is format(,,,... ) Example: int num1 = 34, num2 = 9; int num3 = num1 + num2; formatter.format("%3d + %3d = %5d", num1, num2, num3);

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 7 The format Method of PrintStream Instead of using the Formatter class directly, we can achieve the same result by using the format method of PrintStream (System.out) Formatter formatter = new Formatter(System.out); formatter.format("%6d", 498); is equivalent to System.out.format( "%6d", 498 );

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 8 Control Strings Integers % d Real Numbers %. f Strings % s For other data types and more formatting options, please consult the Java API for the Formatter class.

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 9 Random Number Generation The method Math.random is called a pseudorandom number generator and returns a number of type double that is greater than or equal to 0.0 but less than 1.0. The generated number is called a pseudorandom number because it is not truly random.

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 10 Random Integers The number returned from the random method ranges from 0.0 up to (but not including) 1.0. If we want to generate random integers, we must perform a conversion so the number will fall within the desired range. Use the formula: Y = {X * (max – min + 1)} + min where X is the number returned by random.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 11 Random Integers The formula expressed in Java: //assume correct values are assigned to //'max' and 'min' int randomNumber = (int) (Math.floor(Math.random() * (max-min+1)) + min);

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 12 Random Number Example *Ch6TestRandomGenerator generates N random numbers between 1 and 4 to simulate the suit of a drawn card. It keeps one counter for each suit, and increments the matching counter after a random number is generated. At the end of the generation, it prints the ratio count/N.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 13 Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers of various types –float nextFloat() –int nextInt() –int nextInt(n) produces integer in range 0 to n-1

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 14 Estimating the Execution Time In many situations, we would like to know how long it took to execute a piece of code. For example, –Execution time of a loop statement that finds the greatest common divisor of two very large numbers, or –Execution time of a loop statement to display all prime numbers between 1 and 100 million Execution time can be measured easily by using the Date class.

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 15 Using the Date Class Here's one way to measure the execution time Date startTime = new Date(); //code you want to measure the execution time Date endTime = new Date(); long elapsedTimeInMilliSec = endTime.getTime() – startTime.getTime();

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 16 Using the System Class The System class has static methods called currentTimeMillis and nanoTime that you can also use for timing code –static long currentTimeMillis() –static long nanoTime() long startTime = System.currentTimeMillis(); //code you want to measure the execution time long elapsedTimeInMilliSec = System.currentTimeMillis() – startTime;

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 17 Testing for valid data with Scanner The Scanner class has some methods you can use to check for the right kind of data. boolean hasNext() boolean hasNextInt() boolean hasNextDouble() You can use a loop to read for as long as there is data of the proper type.

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 18 Reading to end of valid input Scanner kbd = new Scanner( system.in); int value; while ( Scanner.hasNextInt()) { value = kbd.nextInt(); process data }


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 1 Chapter 6 Confirmation Dialog Formatting Output."

Similar presentations


Ads by Google