CS 124, Spring 2013
Lab 3: Loops and Branches

This week, you will write your first programs with while loops and if statements.

You should start by creating a new folder named lab3 inside your cs124 folder. All your work for this lab should be done in the lab3 folder.

You will need a copies of TextIO.java and SecondGraphics.java. You should copy them from /classes/cs124 into yoru lab3 folder. (If you don't remember how to create the directory you need or how to copy the files, look back at the instructions at the beginning of Lab 2.)

Your work for this lab is due next Thursday, at the beginning of Lab 4. You should copy your lab3 folder into your homework folder, using either the GUI interface or command line. Make sure that you do all four exercises.

Don't forget about style!!

Exercise 1: Getting Started with While

For exercise 1, write a program that keeps asking the user to guess its name. It should repeat this until the user answers Rumpelstiltskin. For example, the user's conversation with the program might look like this, with the user's input underlined:

           Try to guess my name:  Fred
           Try to guess my name:  Wilma
           Try to guess my name:  Barak Obama
           Try to guess my name:  Queen Elizabeth
           Try to guess my name:  Rumpelstiltskin
           You got it!

Exercise 2: Splitting Words

For your second exercise, you should write a program that splits a string into words. More specifically, the program should read a full line of input from the user, using TextIO.getln(). Then the program should print all the characters in that string, one at a time, except that if the character is a space, the program should output a new line instead of a space. For example, if the user's input string is "Seize the day!" then the output will be


You will need to use the string functions str.length() and str.charAt(i).

Exercise 3: Improved Addition Quiz

In class, we discussed the design of a program that would give a 10-question addition quiz to the user. The program makes up random addition problems and asks the user to solve them. If the user gets a problem correct, the program tells them that the answer is correct; otherwise, it tells them the right answer. Using some reasonable values for the two numbers in the problem, the program can be written as:

        int a, b;
        String problem;
        int userAnswer, correctAnswer;
        int problemNumber;

        problemNumber = 1;
        while (problemNumber <= 10) {
            a = (int)( 1 + 99*Math.random() );
            b = (int)( 41*Math.random() );
            correctAnswer = a + b;
            problem = "What is " + a + " + " + b + " ?";
            userAnswer = TextIO.getlnInt();
            if ( userAnswer == correctAnswer ) {
                System.out.println("That is correct!");
            else {
                System.out.println("Sorry, the correct answer is " + correctAnswer);
            problemNumber = problemNumber + 1;

You should start with this program; you can copy-and-paste it into your text editor. Then, you should extend the program in the following ways.

Your improved program will give the user two chances to get the problem correct. That is, if their first answer is incorrect, then they get another chance to answer the question.

Furthermore, the program should keep score. Each problem is worth 10 points. If the user gets the correct answer on the first try, they get 10 points for the problem. If the user gets the problem correct on the second try, they get 5 points. If they give two wrong answers, they get zero points. At the end of the quiz, you should tell the user their score.

Exercise 4: Random Drawing

The program SecondGraphics.java is another example of using Java to draw pictures. In this case, however, the picture is redrawn every five seconds. By incorporating some randomness into the drawing subroutine, you can get a somewhat different picture each time it is redrawn. As it stands, the program is very dull: It just draws a 100-by-100 square at a random location.

For exercise 4, you should draw a program that draws a large number of differently colored shapes scattered randomly around the image. You should select the location and the color at random. Furthermore, you should draw both circles and squares; each time you want to draw a shape, you should decide at random whether to draw a circle or a square.

Here is a reduced-size picture from my version of the program:

In my version, I use random "translucent" colors, which means that the shapes are slightly transparent. Here is how you can set a color with random amounts of red, green, blue, and transparency:

int red, green, blue, alpha;
red = (int)( 255*Math.random() );
green = (int)( 255*Math.random() );
blue = (int)( 255*Math.random() );
alpha = (int)( 255*Math.random() );
g.setColor( new Color(red,green,blue,alpha) );

If you don't like the transparency effect, you can use just red, green, and blue:

int red, green, blue;
red = (int)( 255*Math.random() );
green = (int)( 255*Math.random() );
blue = (int)( 255*Math.random() );
g.setColor( new Color(red,green,blue) );

You might also like to try using the same alpha value for each color, say alpha = 100. You can try some other variations in the program, if you want. For example, you could choose a random size for the squares and circles. Or you could use rectangles and ovals instead.