CS 124, Fall 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 LoopyGraphics.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 Addition.java, Baseball.java, and LoopyGraphics.java. Submit them using the Submit web page, as usual. Remember that all programs should follow style rules!

Addition Problem: Getting Started with if

The first 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. (Note that this is the only program on this lab that requires TextIO.)

Baseball: Getting Started with While

The program for the second exercise will simulate a baseball game (in a very simple and unrealistic sense). A baseball game consists of a series of "innings". In each inning, two teams get a chance to score "runs". At the end of the game, the team that has the highest total score wins. The program will simulate what happens in a game, but it will just be making up random numbers of runs for each team; it won't actually simulate individual players or at-bats. Here is sample output from one run of the completed program:

         Inning number 1
            The Yankees score 0 runs
            The Red Sox score 0 runs
         The total score is now Yankees 0 to Red Sox 0

         Inning number 2
            The Yankees score 5 runs
            The Red Sox score 0 runs
         The total score is now Yankees 5 to Red Sox 0

         Inning number 3
            The Yankees score 0 runs
            The Red Sox score 0 runs
         The total score is now Yankees 5 to Red Sox 0

         Inning number 4
            The Yankees score 0 runs
            The Red Sox score 1 runs
         The total score is now Yankees 5 to Red Sox 1

         Inning number 5
            The Yankees score 0 runs
            The Red Sox score 0 runs
         The total score is now Yankees 5 to Red Sox 1

         Inning number 6
            The Yankees score 0 runs
            The Red Sox score 2 runs
         The total score is now Yankees 5 to Red Sox 3

         Inning number 7
            The Yankees score 0 runs
            The Red Sox score 0 runs
         The total score is now Yankees 5 to Red Sox 3

         Inning number 8
            The Yankees score 0 runs
            The Red Sox score 0 runs
         The total score is now Yankees 5 to Red Sox 3

         Inning number 9
            The Yankees score 0 runs
            The Red Sox score 3 runs
         The total score is now Yankees 5 to Red Sox 6


         The final score is Yankees 5 to Red Sox 6
         The Red Sox win.
         

The events in one inning could be simulated by the following code:

inning++;
System.out.println();
System.out.println("Inning number " + inning);

yankeesRuns = (int)( 7*Math.pow(Math.random(),10) );
System.out.println("   The Yankees score " + yankeesRuns + " runs");
yankeesTotal += yankeesRuns;

redsoxRuns = (int)( 7*Math.pow(Math.random(),10) );
System.out.println("   The Red Sox score " + redsoxRuns + " runs");
redsoxTotal += redsoxRuns;

System.out.println("The total score is now Yankees " + yankeesTotal 
           + " to Red Sox " + redsoxTotal);

(The formula used to compute yankeesRuns and redsoxRuns makes it possible for a team to score up to 9 runs in an inning, but makes lower numbers of runs much more likely than bigger numbers. The most likely number of runs in an inning is zero. This is trying to match the pattern of scoring in real games.)

You should write the program, which should be named Baseball. You should use the above code in your program, and you should put that code inside a while loop that will simulate a game containing nine innings. After the loop, the program should print the final score and the winner of the game, as shown in the sample output as given above.

But before you turn in the program, you should make two improvements. The first improvement is to deal with tied games. A baseball game can't end in a tie. If the game is tied, the game will continue, even if nine or more innings have already been played. This is called "going into extra innings." You should modify the program so that it continues as long as the game is tied. This is pretty easy.

The second improvement is harder to do if you only use material from Section 3.1 in the textbook. You might want to wait until we cover the break statement in Section 3.3 before you work on this one. In the ninth inning or later, if the second team (the Red Sox) are ahead in the middle of the inning, then the game ends immediately (since there is no need to play the second half of the inning to know that the Red Sox will win). Modify the program to implement this. If it happens, end the game and output a message to tell the user what is happening.

Random Drawing With a Loop

The program LoopyGraphics.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. To do this, you will have to use an if statement inside a while loop.

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

As a variation in my final version of the program, I actually use random "translucent" colors, which means that the shapes are slightly transparent, so that anything drawn beneath them show through. 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.

For full credit, you should add some such variations to the program. As another example, you could choose random sizes for the squares and circles. Or you could use rectangles and ovals instead. You might try drawing an outline around each shape in black. Here is a picture from a version that uses several of these ideas: