## CS 124, Fall 2017

Lab 4: More Control

You will be using more control structures in this lab, concentrating on *for* loops,
including nested *for* loops. You will write two programs. The first uses graphics
to draw some random "art". In the second, you will write a program to administer an
arithmetic quiz to the user.

You will need a copy of the program RandomArt.java
for the first exercise and a copy of TextIO.java for the second.
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 two files, named
**RandomArt.java** and **ArithmeticQuiz.java**.
Turn them in using the Submit web page,
as usual.

### Part 1: Getting Started With *for*

In the previous lab, you used a *while* loop to make a random picture.
In this lab, you will use *for* loops to make two more kinds of "random art."

Start with a copy of RandomArt.java.
This program has a *paintComponent* method
that is called every five seconds, but it doesn't draw anything in the window.
You will complete the program by adding some code in the *paintComponent* subroutine.
You do not have to do any work outside *paintComponent*().

The *paintComponent* method should start by picking a random number. If the number
is less than 0.5, the program should draw the first kind of random art; otherwise, it should draw
the second kind.

For the first kind of art, you should simply draw a large number (say, 500) of random lines.
You should choose a random color for each line. You can either draw a line between two randomly
selected points, or you can draw a line between the center of the window (or some other
constant point) and a random point. Use a *for* loop. Here are examples of both
approaches, scaled down to 1/3 size:

You have to decide how many random lines to draw. (You might even pick that number at random.) Since the window is 800-by-600 pixels, the coordinates for a random point can be computed as

( (int)(800 * Math.random()), (int)(600 * Math.random()) )

For the second kind of art, you are required to use a pair of *nested for loops*. You should
draw a grid of randomly colored squares. Each square should be 100-by-100 pixels, so there are
8 squares across and 6 down in the grid. Furthermore, inside each square, you should draw some
smaller figure such as a smaller square, a circle, or maybe a randomly selected letter.
Here is an example using a randomly colored circle inside each square:

For my own program, I used random HSB colors instead of random RGB colors.
An HSB color has three color components, representing "hue," "saturation," and "brightness."
The hue is the basic color. The saturation tells how pure it is (for example, red is fully
saturated, while pink has the same basic hue but is unsaturated). And the brightness is
what it sounds like. The three components are specified as values of type *float* (not *double*)
in the range 0.0 to 1.0. You don't really need to understand this. Here is how to use
a bright, fully saturated random color:

g.setColor( Color.getHSBColor((float)Math.random(),1,1) );

and here is how to use a darker, but still saturated, random color:

g.setColor( Color.getHSBColor((float)Math.random(),1,0.5F) );

In my artwork, I used a dark color for the squares and a bright color for the circles. I also drew a black outline around each square, since I thought it looked better with the outlines.

### Part 2: Arithmetic Quiz

In Lab 3, you wrote a simple program that asks the user to do an
addition problem. For the second part of this lab, you will write a program that administers
a ten-question arithmetic quiz to the user. **The name of the file that you create must be
ArithmeticQuiz.java**.

I suggest that you first write a program in which all the questions are addition problems. Then later, you can improve the program so that it asks a mixture of addition, subtraction, multiplication, and division problems. For each problem on the quiz, the type of problem should be selected at random.

The program will keep track of the user's score and will report the user's score at the end. The user gets up to two tries to answer a question. If the user answers the question correctly on the first try, the user gets 10 points for the problem. If not, then the user gets a second try. If the user answers the question correctly on the second try, the user gets 5 points for the problem. If the user gets the problem wrong on both tries, the program should tell the user the correct answer. After the end of the quiz, the program should report the user's total score.

The quiz should be appropriate for a young student just learning to do arithmetic.
In particular, the numbers involved should be fairly small positive integers. For a
**subtraction** problem, the answer should not be negative; so in a problem A - B,
you should make sure that B is less than or equal to A. For a **division** problem
A / B, the denominator (B) should evenly divide into the numerator (A) with no
remainder; the easiest way to ensure that is probably to pick B and
the answer at random and then compute A by multiplying B times the answer.
Also, don't forget that division by zero is not allowed.