COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Sample Development: HiLo Game *Chapter 6: Console version *Chapter 7: GUI version
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Hi-Lo Game *Objective is to guess a secret number between 1 and 100. *After each guess, program will say if guess was high or low. *Maximum number of guesses is 6.
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Problem Statement *Write an application that plays the Hi-Lo game with the user *User should be able to play as many games as he/she wants. do { generate secret number play one game } while (user wants to play);
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Development Steps (Chapter 6) 1.Skeleton Ch6HiLo class 2.Code for playing game 3.Random number generation 4.Finalize the code
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Skeleton Class *Make an instantiable main class *Main method creates a HiLo object *HiLo class has a start method describeRules(); ans = prompt( "Play?"); while (ans == "yes") { generateSecretNumber(); playGame(); ans = prompt( "Play?"); }
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Testing *Put print statements in all methods *Run program with 0, 1 and more than one game
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Develop playGame method *Use a dummy secret number Used by more than one method *Need a counter to keep track of the number of guesses guessCount = 0; do { getNextGuess(); guessCount++; // test for hi or lo } while (guessCount<maxGuesses && guess != secretNumber); // Report win or lose
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Testing Step 2 *Test with out of range numbers ( 100) *Test with numbers between 2 and 99 *Test with 1 *Test with 100
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Generate a random number *Write generateSecrtetNumber method Add a print statement to display the random number *Write a separate test class to check the code that goes into the method
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Finalize *Write describeRules *Remove testing code *Check for omissions *Re-test the entire program
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. GUI HiLo Game (Chapter 7) *Use a graphical User Interface and event driven logic *Classes to write Ch7HiLoFrame Ch7HiLo *Development Plan 1.Create skeleton Ch7HiLoFrame class with menus 2.Design and implement GUI layout 3.Implement game-playing logic 4.finalize
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Ch7HiLoFrame *extends JFrame *implements ActionListener *Menus Game New Quit Help GameRules About
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Step 1 *Skeleton class *Create the menus *Implement the quit option *Print name of other menu items *Test by selecting all menu items
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. GUI Layout *Use JTextField for entering guess *Use Jlable to report Hi or Lo *Two Jbuttons Guess to enter guess Cancel to clear the JTextField
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Sample GUI Program
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Step 2 *Add components to frame *Put stubs for remaining menu items *Test by selecting all menu items, clicking buttons and entering something in the JTextField
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Step 3 *Implement the Ch7HiLo Class Similar to Ch6HiLo except that we need methods that can be called from the Ch7HiLoFrame class *Create a Ch7HiLo object in the Ch7HiLoFrame class *Call the newGame method when New menu item is selected
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Step 4 *Implement the Help menu items *Do the final testing