P3: Higher/Lower

COMP 14-091 Summer Session I 2000

 
 


Assigned: Thursday, June 1

Due: Tuesday, June 6, at the beginning of class!
 
 

Description

In this exercise you will write a program that plays the game Higher/Lower. The program should do the following:
Announce that this program plays the game Higher/Lower. Then ask the player for his or her name and read it in. Greet the player and wish them luck. Then your program will select a number between 1 and 100 at random. Tell the user to guess the number. Read in the guess and tell them whether the actual number is higher or lower. If the guess was the actual number then tell them that they won and report the number of guesses it took them to guess the number. Also, use a sentinel value to let them quit the game if they don't want to continue playing. At the end of each game (by quitting, or correct guess), prompt to see if the user would like to play another game. Keep playing games until the user decides to stop. Don't forget to check the input to determine if it's in the valid range of 1 to 100 and prompt them again if it's not. Assume they will actually type in a number and don't worry about what happens if they type in a name or other such foolishness.
EXTRA CREDIT: For extra credit you can keep track of the number of guesses that the user made and report this when they win or quit. (So you would say something like "You won! It took you 15 guesses to find the number." when they won or "You have chosen to quit the game after taking 3 guesses." when they quit)

Steps to Follow

  1. Launch Microsoft Visual J++ 6.0 and create a new project. Name this project P3. (HINT: if you set the location to be A:\ then it will make it right on the floppy disk and put the project in a directory called P3 - just the way I want it turned in!)
  2. Add a new "ClassMain" and call it HigherLower.java
  3. This time you will have to build your program by yourself instead of copying and filling in.
  4. Make sure you start the file with a big block comment that includes the following:
  5. Put all your code inside the braces for main.
  6. As the very last statement in your program (right before the closing brace) put this: System.out.println("\nEnd of job"); This will tell me that your program is done executing.
  7. Don't forget to comment your code!
  8. Save often!

What to turn in

Almost like last time (don't forget to keep a copy for yourself!):

Getting input from the user

In order to use the input statements we talked about in class, there are a few things you have to do. First, add the following statement to the top of your program, right after the block comment that has the program name, your name, etc. Look at P1 and P2 to see where I put the import statement for those two programs.
import java.io.*;
Next you have to change the line with the main method header from this
public static void main(String[] args)
to this
public static void main(String[] args) throws IOException
Don't forget that the capitalization is important! Now you can go ahead and use the statements we talked about in class. First, you have to put the setup code that we talked about at the top of main, right after the opening brace:
BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ));
Then, you need to declare a variable for whatever type of data you want to read in and use the correct statement to read in that data type:

For String:

String name;
name = stdin.readLine();
For int:
int number;
number = Integer.parseInt(stdin.readLine());

How do I generate a random number?

See page 81 for the book's description of generating random numbers. Here's how I would do it:

First, put the following at the top of the file, right after the block comment that has the program name, your name, etc. Look at P1 and P2 to see where I put the import statement for those two programs.

import java.util.Random;
Next, use the following code to create an object of class Random (this would go near the beginning of the main method):
Random randomNum = new Random();
Then, this line will generate a random number and store it in a variable called myNum (change myNum to whatever you call your variable):
myNum = Math.abs(randomNum.nextInt()) % 100;   // generates an integer from 0 to 99
myNum = myNum + 1;   // adding 1 makes it so the numbers are from 1 to 100

Notes

Note: this is only an example. Your work does not need to match the example word for word.

Here's an example of the sort of thing your program should do if there is incorrect input (once again, it doesn't have to match word for word). Remember that the message saying the number of guesses is extra credit. You have to print out a message saying that they chose to quit, but it doesn't HAVE to indicate the number of guesses.

Sample output with the extra credit done can be found here.