Recall that to obtain a value from the user, there are some special lines of code that must be added to your program. Here is a very simple, short example of a program that would ask the user for an integer value and then print it:
import java.util.Scanner; // This line *must* come first. public class ExampleInput { // Declare the variable "keyboard" for use throughout the program. public static Scanner keyboard = new Scanner(System.in); public static void main (String[] args) { // Print a prompt, get the value from the user, // and then print that value. System.out.print("Enter an integer: "); int x = keyboard.nextInt(); System.out.println("You entered: " + x); } // end main() } // end ExampleInput
if-then-else statements allow us to make decisions about which code to run based on the outcomes of certain conditions. Thus, we refer to them as conditional statements. However, we saw in class that sometimes, we want certain code to repeat based on certain conditions. These loop statements (or just loops) allow us to write code that repeats as many times as we like, depending on a condition.
To see how a loop is written, here is a simple example of a program that asks a user to enter a positive integer. It will ask the user over and over again until that positive integer is provided:
import java.util.Scanner; public class ExampleLoop { public static Scanner keyboard = new Scanner(System.in); public static void main (String[] args) { int x = 0; do { System.out.print("Enter a positive integer: "); x = keyboard.nextInt(); } while (x <= 0); System.out.println("Thank you! You enetered: " + x); } // end main() } // end ExampleLoop
There are special algorithms, known as psuedorandom number generators, that can pick unpredictable values that have nothing to do with what your program has done so far, and nothing to do with what a user may have input to your program. These algorithms are very handy for certain kinds of simulations and game-playing programs that we will see later. Java provides a command that generates such a (seemingly) random value:
double randomValue = Math.random();
After this line of code is carried out by the machine, the variable randomValue will contain some unpredictable value between 0.0 and 0.999... (arbitrarily close to, but never exactly, 1.0). The ability to select a random number will be critical for this assignment.
The question that should ask yourself is, How does a deterministic machine pick a random value? Part of the answer lies in the name of these algorithms -- notice that they are not random number generators, but psuedorandom number generators. That is, the number sure seem random, but they're produced by a deterministic process. We'll talk more about this issue next week.
You are to write a program, from scratch, that has its user play the following game: Your program picks a random number between 1 and 1000 (inclusive). The user then tries to guess the number. If the user guesses the value correctly, the program says so and ends. If the user guesses incorrectly, the program tells the user whether the guess was too high or too low, and then allows the user to guess again.
First, if you don't remember how to connect to or use the UNIX servers, refer back to lab-1 to remind yourself.
To get started on this lab, follow these steps:
Login to remus or romulus using VNC.
Open a terminal window to get a shell prompt.
Make a new subdirectory for this lab by using the following command:
mkdir lab-2
Change into that subdirectory so that all of your files are created within it:
cd lab-2
Open a new Java source code file using emacs. Don't forget the trailing ampersand.
emacs GuessTheNumber.java &
Write a program named GuessTheNumber within this file! When you think that you have something that might work, click over to your terminal window, and use the javac command to compile it, followed by the java command to run it. If you find bugs, fix them, compile, and run again.
When your work is complete, use the cs11-submit program again. For this lab, you will submit your work with the following command at the shell prompt:
cs11-submit lab-2 GuessTheNumber.java
As always, be sure to check the output of the cs11-submit program carefully to be sure that it did not indicate any errors. If the program completes without an error message, than it was submitted successfully.