CS 124, Spring 2014
Lab 4: More Control

This lab consists of three more programs that use control structures. The first two exercises are short; they require you to use for loops. The third and more substantial exercise can be done using whatever techniques you find convenient.

Start, as usual, by making a directory named lab4. You will need a copy of TextIO.java in that directory, which you can copy from /classes/cs124 or from one of your previous lab directories. That's the only file you will need. All of the exercises on this lab ask you to write programs from scratch. Only the first program needs input from the user.

Your work for this lab is due next Tuesday, at the beginning of the next lab. You should copy your lab4 folder into your homework folder as usual.

Don't forget about style!!

Exercise 1: Approximating π

For your first exercise with for loops, you will write a short program that uses random numbers to find an estimate for the mathematical constant π (which is also available in Java, as an approximation, in the constant Math.PI). I expect to have explained in class why the estimate works.

Use a for loop to do the following for one billion (1000000000) repetitions: pick two random numbers x and y, in the range 0.0 to 1.0. Compute x*x + y*y, If the result is less than 1, then add 1 to a counter. (That is, you want to count the number of times that x*x + y*y is less than 1.)

After the for loop, divide the counter by one billion and multiply by 4. This is your estimate for the number π.

Print out your estimate for π along with the value of the built-in constant Math.PI for comparison.

(Note: The program will take some time to run. Depending on the speed of your computer, it might be more than a minute. Almost all of the time is spent computing random numbers.)

Exercise 2: Words, Words, Words

For the second exercise of the lab, write a program that does this: Read a line of input from the user into a variable of type String. Use a for loop to go through the string character-by-character. If the character is a letter, copy it to the output; if the character is a space, output a new line; otherwise, don't do anything with the character. The effect is to output one word per line, where a word is defined as a consecutive sequence of letters. (There is an assumption here that the user does not put more than one space between letters. You can make that assumption.)

At the end, you should state how many words were output. Try to count correctly; you want the number of words.

For example, if the input is "Neither a borrower, nor a lender, be!", then the output should look like this:

                 The number of words was 7

Exercise 3: Play Ball!

Baseball 101: A baseball game between two teams consists of 9 or more "innings". An inning has two parts. In the first part, the "visiting" team bats and might score some "runs"; in the second part, the "home" team bats and might score some "runs". The team that scores the most runs in total wins. If the "home" team is already ahead in the middle of the ninth inning, then the home team doesn't bat because it wouldn't affect the outcome. If the score is tied at the end of the ninth inning, then the game continues into extra innings. The game will then end at the end of the next inning when the score is not tied.

Write a program that simulates a baseball game in the following sense. In each inning, each team scores some random number of runs. Add the number of runs to the team's score. After each inning, report the number of runs scored by each team in that inning and report the score after each inning. At the end, say which team won. The output should look more or less like this:

            Inning 1:
                The Yankees score 1
                The Red Sox score 2
                Score at the end of the inning:  1 to 2
            Inning 2:
                The Yankees score 0
                The Red Sox score 0
                Score at the end of the inning:  1 to 2
            Inning 3:
                The Yankees score 4
                The Red Sox score 0
                Score at the end of the inning:  5 to 2
             Inning 9:
                The Yankees score 0
                The Red Sox score 1
                Score at the end of the innning:  5 to 6
             The Red Sox win.

You don't have to use "Yankees" and "Red Sox" as the team names.

To implement the logic of this program, it is probably easiest to use a "while (true)" loop. But there are many ways to organize the code. You should design a program that makes sense to you. Start by making sure that you understand the process that the program must go through when it is executed. Consider writing a pseudocode outline before you start coding.

You should think about the best way to give a team a "random number of runs." Just picking a random number between, say, 0 and 5, is a possibility, but not very realistic. You might think about trying for something more realistic. For example, the most likely number of runs in an inning is zero, and most games end with rather low scores. But occasionally, a team might score a large number of runs in an inning. Ideally, you will come up with a technique for generating random runs that has similar properties. You should try for a reasonably realistic simulation. (Extra credit might be given for particularly nice approaches.)