CS 124, Fall 2011
Lab 3: Control!

In this lab, you will start working with if statements and while statements. There are three fairly short exercises that are due next week. And at the end of the lab, you'll find a larger project that will be due in two weeks.

To start the lab, make a lab3 directory inside your cs124 directory. (Please use the name "lab3" exactly.) Copy TextIO.java into the lab3 directory. (If you don't remember how to do this, see Lab 2.)

The three exercises from this lab are due at the beginning of next week's lab. To turn in your work, copy your lab3 folder into your homework folder inside /classes/cs124/homework.


Getting Started With if

Exercise 1: The first exercise is a short program that uses an if statement. Your program should select two random integers in the range from 10 to 99. There are 90 numbers in that range, so you can generate each random number with the formula

               10 + (int)(90 * Math.random())

Your should show the user the two numbers and ask the user to compute the sum. Read the user's response. If the user's response is correct, tell the user that the response is correct. Otherwise, tell the user that the response is wrong and tell them the correct answer.

Don't forget to make the output look nice. The conversation with the user should look something like this:

            Compute the following sum:
            
                 42
               + 17
               ------
              
            Enter your answer here:  69
            Sorry, your answer is incorrect!
            The correct answer is 59.

Getting Started With while

Exercise 2: For the second exercise, you will write a short program that uses a while loop. Your program should start by asking the user to type in an integer greater than or equal to 2. (You can assume that the user's input satisfies this condition, but you might want to think about what your program will do if the user enters a smaller number. Hint: If a program seems like it has entered into an infinite loop, you can force the program to stop by typing a CONTROL-C in the Terminal window where the program is running.)

After getting the user's input, you want to print out all powers of the input number that are less than or equal to two billion (2000000000). That is, you want to start with 1, and then keep multiplying by the input number, as long as the result is less than or equal to 2000000000. For example, if the user's input is 13, then the output would be:

            13
            169
            2197
            28561
            371293
            4826809
            62748517
            815730721

To write the program, you need a variable to represent the product. The value of product starts at 1. Each time through the loop, multiply the product by the user's input number, with a statement such as

product = product * inputNumber;

If and while Together

For making more complex programs, it's useful that statements can be nested inside other statements. You can have if statements inside of while statements, while statements inside of if statements, while statements inside of other while statements, and so on. For the final exercise of the lab, you will write a program that uses an if inside a while.

Exercise 3: Write a program that simulates the following experiment: Roll three dice over and over. Roll them thirty-six million (36000000) times. Count the number of times that the roll is three of a kind (that is, die1 == die2 and die2 == die3). At the end, output the number of times that the roll was a three of a kind. On the average, you can expect one three-of-a-kind for each 36 rolls, so the output should be something close to 1000000.

To write the program, you will need a "counting loop." You have to count the number of times that the dice are rolled, and continue as long as that number is less than or equal to 36000000.


Longer-term Project Number 1

This section describes a project that is longer and more complex than the usual lab problems. This project is not due with the rest of Lab 3. Instead, it will be collected separately, in two weeks. We will discuss the project in class. However, you can begin thinking about the problem and working on the program design now.

For this assignment, create a directory named proj1 in your cs124 directory. Please use exactly the name "proj1". Copy TextIO.java into the proj1 directory, and put your work for this project in the same directory. When the project is finished, you should copy your proj1 directory into your homework folder in /classes/cs124/homework. You should do that by 2:00 PM on Friday, September 30.

The project is to write a program that will administer a ten-question arithmetic quiz to the user. There can be addition, subtraction, multiplication, and division problems. The questions should be appropriate for elementary school students who are just beginning to learn arithmetic. For example, all the numbers should be integers. The number of digits for addition problems should be at most two. For multiplication problems, at least one of the numbers should be a one-digit number. For subtraction, the answer should not be a negative number. For division, the answer should be an exact integer, so that a problem like 28 / 5 would not be possible.

For each of the ten problems, you should pick the kind of problem -- addition, subtraction, multiplication, or division -- at random. You should pick the numbers in the problem at random. So, for an addition problem A + B, you should choose A and B at random. Present the problem to the user and get the user's answer. Compute the correct answer and check the user's response. If the user gets the problem wrong, tell them the correct answer. At the end of the test, tell the user how many problems they got right.

(Hint: For a division problem of the form A / B, you can pick B at random and also pick the answer at random; then use those two numbers to compute the value of A. This will give a division problem where the answer is an exact integer.)

You should be trying for more than just a program that works! The program should have I/O that looks nice and makes it easy for the user to tell what is going on. It should be friendly to the user. (This is for kids!) The program itself should, of course, be nicely formated and commented and should follow all the other rules of good programming style.

Extra credit opportunity: Give the user two chances to get each problem correct. If they get it right on the first try, they get full credit (10 points). If they get it right on the second try, they get half credit (5 points). If they don't get the correct answer in two tries, tell them the correct answer, and don't give them any points. At the end of the quiz, instead of telling the user how many problems they got right, tell them how many points they got, out of a possible 100.


Remember that the work that you turn in for this assignment should be your own. Do not work with other people in the class. Do not discuss your work with anyone except for the professor and the TAs.