## CS 124, Spring 2021 Lab 3: While and If Statements

For this lab, you will write three programs that use while loops and if statements. (Even if you know how to use other kinds of loops, you should use while loops for this lab!) Note that none of the programs require user input, so you will not need TextIO for this lab. The second program uses JavaFX, so you will need an Eclipse project that supports JavaFX.

This lab will be due by the end of the day next Tuesday, and it will be submitted using the program submission website, math.hws.edu/submit, as usual. It will also be accepted late until noon on Saturday, but there will be a 10% penalty for labs that are turned in late without some good reason. You will be required to turn in three programs named CoinFlips.java, LoopyGraphics.java, and Baseball.java.

Remember that all programs are required to follow the rules of good programming style!! And remember that you are writing your own solutions to the exercises. Although you can discuss the exercises with other students in general terms, you need to construct your own solutions. If you do that, it is not possible that your programs will look very similar to anyone else's.

### Exercise 1: Flipping a Coin

One String function that was not mentioned in class is str.contains(s). You will need that function for this exercise. If str and s are Strings, then str.contains(s) is true if str contains s as a substring; if not, it is false. For example, if str is "concatenate", then str.contains("cat") is true, but str.contains("dog") is false.

Your program file for this exercise must be named CoinFlips.java. Start by creating a new class named CoinFlips.

Write a program that simulates flipping a coin 64 times, and makes a string of H's and T's to record the result. That is, when the simulated coin comes up heads, add a 'H' to the string, and when it comes up tails, add a 'T' to the string. Then, test whether the coin ever came up heads six times in a row, by testing whether the string contains HHHHHH as a substring. At the end, output the string of H's and T's, and output the result of the test. The output of the program will look something like this:

```HTTHHTTTHTHTHHTTTHTTHHHHTTTHTTTHTTHTHHHTHHHHTHTHTTHTHTTTTTTTHHHH
The string does NOT contain HHHHHH
```

Note: In this lab, it will be very easy for you to write programs that go into infinite loops. I did that a couple times myself while writing solutions. The program will just keep running forever—or until you quit Eclipse—unless you stop it. You can force a program to stop by clicking the small, red, square stop button in the header of the Console pane:

If you leave several programs running, they might be stacked up in the Console pane. After a program has stopped, you can click the X next to the red stop button to close the information for that program run.

### Exercise 2: Loopy Graphics

This exercise uses JavaFX. You should start with a copy of the file LoopyGraphics.java. The file that you turn in for this exercise must be named LoopyGraphics.java.

Once again, you will be working entirely inside a subroutine named drawPicture. Your assignment this time is to fill the picture with a "gradient" and then to add some large number of things to the picture. You could, for example, draw something like this, which is supposed to look vaguely like the horizon just after sunset with a full moon:

The background in this image is a gradient because the color gradually changes from one color to another, from the top of the image to the bottom. In this case, the gradient was created by drawing 120 rectangles that fill the picture. Each rectangle is 800 pixels wide and 5 pixels high. The amount of red in the color changes from one rectangle to the next, while the amount of blue is constant. Recall that you can set the fill color to have a given amount of red, green, and blue by calling g.setFill(r,g,b) where r, g, and b are real numbers in the range 0.0 to 1.0.)

For the sample image, I drew 200 small white ovals at random locations. (LoopyGraphics is set to redraw its picture whenever the user clicks the picture, and the stars will be in different positions each time the the user clicks.)

You can try to duplicate my picture, or you can choose to create something more original. One idea is simply to enhance it by using several colors for the stars. Your picture does not necessarily have to resemble anything real. The requirement is only that you draw a gradient and then add a large number of something (and that you do these things using loops, not using hundreds of statements!).

### Exercise 3: Simulated Baseball Game

The program for the second exercise will simulate a baseball game (in a very simple and unrealistic sense). The file that you turn in for this exercise must be named Baseball.java, and you should start by creating a new class named Baseball.

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. Usually, there are nine innings, but there are some complications: First, if the score is tied at the end of the ninth or later inning, the game continues. The game can only end when one team is ahead. There is no limit on how many innings there can be. Second, in the ninth inning, if the home team (the one that goes second in the inning) is ahead in the middle of the inning, then the game ends at that point, since giving the home team a chance to score at that point cannot make any difference to the outcome. This can only happen in the ninth inning.

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.
```

Another possible ending for the game is:

``` Inning number 9
The Yankees score 0 runs
the Red Sox score 2 runs
The score is tied 5 to 5, so the game continues

Inning number 10
The Yankees score 0 runs
the Red Sox score 0 runs
The score is tied 5 to 5, so the game continues

Inning number 11
The Yankees score 0 runs
the Red Sox score 4 runs
The total score is now Yankees 5 to Red Sox 9

The final score is Yankees 5 to Red Sox 9
The Red Sox win.
```

And yet another is:

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

Inning number 9
The Yankees score 2 runs
The total score is now Yankees 7 to Red Sox 8
The game ends because the Red Sox are ahead.

The final score is Yankees 7 to Red Sox 8
The Red Sox win.
```

The output of your program should be similar. You should show the number of runs scored in each inning. At the end, you should say who won. And you should take into account all of the ways that the program might end. One way to organize your program is to use two loops, one for the first 9 innings and one for any extra innings. It is also possible to do it all in one loop, but you will need to be a little more clever about determining when the loop should end.

Note that in baseball, the most common number of runs in an inning is zero, and large numbers of run are unlikely. The web page

https://gregstoll.com/~gregstoll/baseball/runsperinning.html

has some statistics on the number of runs scored in individual innings in actual games. For example in over 70% of innings, a team scores zero runs. For full credit for this exercise, your program should at least make zero the most likely number of runs that a team can score in an inning. If you can do an especially good job of making the distribution of runs more realistic, you might get a little extra credit.