Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101X: Programming Methodology Recitation 3 Control Structures.

Similar presentations


Presentation on theme: "CS1101X: Programming Methodology Recitation 3 Control Structures."— Presentation transcript:

1 CS1101X: Programming Methodology Recitation 3 Control Structures

2 CS1101X Recitation #32 Problem Statement Write an application that will play Hi-Lo games with the user. The objective of the game is for the user to guess the computer-generated secret number in the least number of tries. The secret number is an integer between 1 and 100, inclusive. When the user makes a guess, the program replies with HI or LO depending on whether the guess is higher or lower than the secret number. The maximum number of tries allowed for each game is six. The user can play as many games as she wants.

3 CS1101X Recitation #33 Overall Plan Tasks: do { Task 1: generate a secret number; Task 2: play one game; } while ( the user wants to play );

4 CS1101X Recitation #34 Required Classes Ch6HiLo main class JOptionPane Math standard classes

5 CS1101X Recitation #35 Development Steps We will develop this program in four steps: 1.Start with a skeleton Ch6HiLo class. 2.Add code to the Ch6HiLo class to play a game using a dummy secret number. 3.Add code to the Ch6HiLo class to generate a random number. 4.Finalize the code by tying up loose ends.

6 CS1101X Recitation #36 Step 1: Design The topmost control logic of HiLo 1. describe the game rules; 2. prompt the user to play a game or not; while ( answer is yes ) { 3. generate the secret number; 4. play one game; 5. prompt the user to play another game or not; }

7 CS1101X Recitation #37 Step 1: Code (1/5) Directory: Chapter6/Step1 Source Files: Ch6HiLo.java Directory: Chapter6/Step1 Source Files: Ch6HiLo.java

8 CS1101X Recitation #38 Step 1: Code (2/5) /* Chapter 6 Sample Development: HiLo Game (Step 1) File: Step1/Ch6HiLo.java */ import javax.swing.*; class Ch6HiLo { // Default constructor. public Ch6HiLo( ) { } //Main Method public static void main (String[] args) { Ch6HiLo hiLo = new Ch6HiLo( ); hiLo.start(); }

9 CS1101X Recitation #39 Step 1: Code (3/5) /** * Top level method that calls other private methods * to play the Hi-Lo games. */ public void start ( ) { int answer; describeRules(); answer = prompt("Do you want to play a Hi-Lo game?"); while (answer == JOptionPane.YES_OPTION) { generateSecretNumber( ); playGame(); answer = prompt("Do you want to play another Hi-Lo game?"); }

10 CS1101X Recitation #310 Step 1: Code (4/5) // Provides a brief explanation of the program to the user. private void describeRules( ) { System.out.println("Inside describeRules"); //TEMP } // Generates a random number between 1 and 100. private void generateSecretNumber( ) { System.out.println( "Inside generateSecretNumber"); //TEMP } // Plays one Hi-Lo game. private void playGame( ) { System.out.println("Inside playGame"); //TEMP }

11 CS1101X Recitation #311 Step 1: Code (5/5) // Prompts the user for a yes-no reply. private int prompt(String question) { int reply; reply = JOptionPane.showConfirmDialog(null, question, "Confirmation", JOptionPane.YES_NO_OPTION); return reply; }

12 CS1101X Recitation #312 showConfirmDialog method Refer to API Specification: JOptionPane classJOptionPane static int showConfirmfDialog(Component parentComponent, Object message, String title, int optionType) Brings up a dialog where the number of choices is determined by the optionType parameter. optionType Defines the set of option buttons that appear at the bottom of the dialog box: * DEFAULT_OPTION * YES_NO_OPTION * YES_NO_CANCEL_OPTION * OK_CANCEL_OPTION You aren’t limited to this set of option buttons. You can provide any buttons you want using the options parameter.

13 CS1101X Recitation #313 Step 1: Test In the testing phase, we run the program and verify confirm that the topmost control loop terminates correctly under different conditions. Play the game  zero times  one time  one or more times

14 CS1101X Recitation #314 Step 2: Design Implement the playGame method that plays one game of HiLo. Use a dummy secret number  By using a fix number such as 45 as a dummy secret number, we will be able to test the correctness of the playGame method

15 CS1101X Recitation #315 The Logic of playGame int guessCount = 0; do { get next guess; guessCount++; if (guess < secretNumber) { print the hint LO; } else if (guess > secretNumber) { print the hint HI; } } while (guessCount < number of guesses allowed && guess != secretNumber ); if (guess == secretNumber) { print the winning message; } else { print the losing message; }

16 CS1101X Recitation #316 Step 2: Code (1/5) // Chapter 6 // File: Step2/Ch6HiLo.java import javax.swing.*; class Ch6HiLo { private final int MAX_GUESS_ALLOWED = 6; private final int LOWER_BOUND = 1; private final int UPPER_BOUND = 100; private int secretNumber; // Default constructor. public Ch6HiLo( ) { } // Main Method public static void main (String[] args) { Ch6HiLo hiLo = new Ch6HiLo( ); hiLo.start(); }

17 CS1101X Recitation #317 Step 2: Code (2/5) // Top level method that calls other private methods public void start ( ) { describeRules(); int answer=prompt("Do you want to play a Hi-Lo game?"); while (answer == JOptionPane.YES_OPTION) { generateSecretNumber( ); playGame(); answer = prompt("Do you want to play another Hi-Lo game?"); } // Provides a brief explanation of the program to the user. private void describeRules( ) { System.out.println("Inside describeRules"); //TEMP }

18 CS1101X Recitation #318 Step 2: Code (3/5) // Generates a random number between 1 and 100. private void generateSecretNumber( ) { System.out.println("Inside generateSecretNumber"); secretNumber = 45; //TEMP } // Gets the next guess from the user private int getNextGuess( ) { String inputStr; int input; while (true) { inputStr = JOptionPane.showInputDialog(null,"Next Guess"); input = Integer.parseInt(inputStr); if (LOWER_BOUND <= input && input <= UPPER_BOUND) return input; //invalid input; print error message JOptionPane.showMessageDialog(null, "Invalid Input: " + "Must be between " + LOWER_BOUND + "and " + UPPER_BOUND); }

19 CS1101X Recitation #319 Step 2: Code (4/5) // Plays one Hi-Lo game. private void playGame( ) { int guessCount = 0, guess; do { guess = getNextGuess(); guessCount++; if (guess < secretNumber) JOptionPane.showMessageDialog(null, "Your guess is LO"); else if (guess > secretNumber) JOptionPane.showMessageDialog(null, "Your guess is HI"); } while ( guessCount < MAX_GUESS_ALLOWED && guess != secretNumber ); if ( guess == secretNumber ) JOptionPane.showMessageDialog(null, "You guessed it in " + guessCount + " tries."); else JOptionPane.showMessageDialog(null, "You lost. Secret No. was " + secretNumber); }

20 CS1101X Recitation #320 Step 2: Code (5/5) // Prompts the user for a yes-no reply. private int prompt(String question) { int reply; reply = JOptionPane.showConfirmDialog(null, question, "Confirmation", JOptionPane.YES_NO_OPTION); return reply; }

21 CS1101X Recitation #321 Step 2: Test We compile and run the program numerous times To test getNextGuess, enter  a number less than 1  a number greater than 100  a number between 2 and 99  the number 1 and the number 100 To test playGame, enter  a guess less than 45  a guess greater than 45  45  six wrong guesses

22 CS1101X Recitation #322 Step 3 Design We complete the generateSecretNumber method. We want to generate a number between 1 and 100 inclusively. private void generateSecretNumber( ) { double X = Math.random(); secretNumber = (int) Math.floor( X * 100 ) + 1; System.out.println("Secret Number: " + secretNumber); //TEMP return secretNumber; }

23 CS1101X Recitation #323 Step 3: Code (1/2) // Chapter 6 // File: Step3/Ch6HiLo.java // Generates a random number between 1 and 100. private void generateSecretNumber( ) { double X = Math.random(); secretNumber = (int) Math.floor(X*100) + 1; System.out.println("Secret Number: " + secretNumber); //TEMP }

24 CS1101X Recitation #324 Step 3: Code (2/2) // File: Step3/TestRandom.java class TestRandom { public static void main ( String args[] ) { int N = 1000, count = 0, number; double X; //Experiment by increasing the value for N. do { count++; X = Math.random(); number = (int) Math.floor( X * 100 ) + 1; } while ( count < N && 1 <= number && number <= 100 ); if ( number 100 ) System.out.println("Error: " + number); else System.out.println("Okay"); }

25 CS1101X Recitation #325 Step 3 Test We use a separate test driver to generate 1000 secret numbers. We run the program numerous times with different input values and check the results. Try both valid and invalid input values and confirm the response is appropriate

26 CS1101X Recitation #326 Step 4 Finalize Program Completion  Finish the describeRules method  Remove all temporary statements Possible Extensions  Allow the user to set her desired min and max for secret numbers  Allow the user to set the number of guesses allowed  Keep the score—the number of guesses made — while playing games and display the average score when the user quits the program

27 CS1101X Recitation #327 End of Recitation #3


Download ppt "CS1101X: Programming Methodology Recitation 3 Control Structures."

Similar presentations


Ads by Google