As usual, create a subdirectory for this lab, open up the Web version of this handout in Mozilla Firefox, and open Eclipse.
Exercise #1: Infinite Loops and Loop Counters
Breaking out of infinite loops: One of the first things you need to learn about loops is how to break out of infinite loops! Different systems have different keys for doing this. In Linux, CTRL-C stops a program that is running. In Eclipse, there is a red square on the console window that you click to stop the program.
The program in LoveCS.java should print "I love Computer Science!!" as many times as the user wishes. Copy it to your directory and compile and run it to see how it works.
Clearly the program has a problem and it is a typical one when writing loops - there is no update of the loop control variable. Modify the program as follows:
Counter before the loop begins: 1 Loop ... 1 I love Computer Science!! 2 I love Computer Science!! 3 I love Computer Science!! Counter after the loop ends: 4
while (count <= limit); { ... }Run the program and see what happens. Why did the program do what it did?
Remove the semicolon before proceeding!!
Counter before the loop begins: ______ Loop ... 3 I love Computer Science!! 2 I love Computer Science!! 1 I love Computer Science!! Counter after the loop ends: _____
Counter before the loop begins: _______ Loop ... 2 I love Computer Science!! 4 I love Computer Science!! 6 I love Computer Science!! 8 I love Computer Science!! Counter after the loop ends: ______
Exercise #2: Powers of 2
File PowersOf2.java contains a skeleton of a program to read in an integer from the user and print out that many powers of 2, starting with 20.
Here are the first 4 powers of 2: 1 2 4 8
Here are the first 4 powers of 2: 2^0 = 1 2^1 = 2 2^2 = 4 2^3 = 8
Exercise #3: Summing in a Loop
As we have seen in class one task a loop often performs is adding up some values. In this exercise you will complete a program that adds up the points scored in a specified number of games (the number is input to the program) and finds the average number of points scored per game. The basic program will contain a count-controlled loop that reads in scores and adds them up.
Complete the program using the comments as a guide. Run the program several times with different numbers of games to make sure it works.
... Prompt for and read in the input while ( ... the input is not valid ...) { Let the user know the input is not valid Ask them to try again Read in the new value } ... continue the program - now the input is guaranteed to be validSuppose a valid score in this game (whatever it is!) is in the range 0 - 250. Add a validation loop inside the current loop (a loop inside a loop is called a nested loop) to guarantee that the user enters a valid score. The structure of your program will be as follows (you must translate it into correct Java!):
while ( ....... ) // this is your current while statement { ... System.out.print ("Enter the points scored in game " + gameCount): score = scan.nextInt(); // Validate the score while ( ... score is not valid ... ) { Tell the user the score is not correct Ask the user to enter a score in the correct range Read in the new score } // Process the score - from here on out the code is what // you already have
Test your program thoroughly. Make sure it catches all types of incorrect input.
Exercise #4: A Guessing Game
File Guess.java contains a skeleton for a program to play a guessing game with the user. The program should randomly generate an integer between 1 and 10, then ask the user to try to guess the number. As long as the user guesses incorrectly, the program should ask him or her to try again; when the guess is correct, the program should print a congratulatory message.
To submit your code: Tar the files in your lab7 directory (be sure to name the file with your names, not lab7.tgz) and cp the tgz file to the directory:
/home/staff/bouchard/CPSC120B/lab7Extra Credit: Program Testing Submit your code as instructed above before beginning the extra credit. You will receive extra credit for finding errors in your classmates' GameStats program. Links to the programs that have been submitted thus far can be found here. If you can find an error email your instructor the program number, the input that causes the error, what the program outputs, and what the should output. For each program you find an error you will receive one extra point on your lab. All emails should be sent before class on Friday.