CS 124, Spring 2017
Lab 3: Control

In this lab, you will work with control structures. You will use basic if statements and while loops, as covered in Section 3.1. (Do not use "for loops" even if you know what they are.) You will need copies of TextIO.java and SecondGraphics.java. As usual, you can find copies in the directory /classes/cs124.

This lab is due at the start of next week's lab. You will submit files named GuessName.java, Addition.java, AdditionQuiz.java, and SecondGraphics.java. Submit them using the Submit124 web page, as usual.

Getting Started with while

For your first exercise using control structures, 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:  Hari Seldon
           Try to guess my name:  Queen Elizabeth
           Try to guess my name:  Rumpelstiltskin
           You got it!

The name of the program should be GuessName.
You are required to use a while loop.

(Remember that you need to use s1.equals(s2) or s1.equalsIgnoreCase(s2) to check whether two strings, s1 and s2, are equal.)

Getting Started with if

The second exercise is to write a program that will ask the user to compute the sum of two small integers. The integers should be randomly chosen. The program should read the user's answer and check it. It should either say that the answer is correct, or it should say that it is incorrect and give the correct answer. A run of the program might look like:

           What is  17 + 5 ?  23
           Sorry, 23 is not correct.
           The correct answer is 22.

or like:

           What is 21 + 32 ?  53
           Congratulations, 53 is correct.

The name of the program should be Addition.

Nested Control Statements

The statements inside control statements can themselves be control statements. For this exercise, you will use an if statement inside a loop. (You are required to use a while loop.) You will also need one if inside another if to get full credit.

The program should administer a quiz to the user. The quiz should consist of 10 randomly generated addition problems. The program should keep score and report the user's score at the end. For full credit, you should give the user two chances to get the question right. If they get it right on the first try, they should receive full credit for the problem. If they get it right on the second try, they should receive half credit. If they get the problem wrong on both tries, they get no credit and the program should tell them the correct answer. (You can grade the quiz either out of a total of 100 points or out of a total of 10 points.)

The name of the program should be AdditionQuiz. The program builds on the previous exercise. You can copy some code from your solution to the previous exercise, but you will turn in two separate programs for the two exercises.

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. To add some interest, the color of the square is also selected at random.

For the last exercise of this lab, you should edit SecondGraphics.java to make a program that draws a large number of differently colored shapes scattered randomly around the image. You should select the location and the color of each shape at random. You are required to use a while loop to count the shapes. Furthermore, you should draw two different shapes, such as 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:

The program already has an example of setting a random color, and we looked at how to do it in class. In my picture, I actually use random "translucent" colors, which means that the shapes are slightly transparent. Here is how you can set a color with a random amount of transparency as well as random amounts of red, green, and blue:

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) );

Another possibility is to use some constant amount of transparency, say alpha = 100.

You can try some other variations in the program, if you want. For example, you could choose random sizes for the squares and circles. Or you could use rectangles and ovals instead. However, you must have at least two different kinds of shape. You might try drawing a filled translucent shape, then drawing a border around it in solid opaque color.