# Lab: Histograms

## Using Loops

Last week, two of the discussion questions were:+

• One can simulate the movement of a fish in an aquarium by repeatedly flipping a coin. When "heads" is flipped, the fish moves one foot to the right; when "tails" is flipped, it moves one foot to the left. (We are assuming a long, narrow aquarium.) Assume that the fish's initial location is labeled 0 and that locations to the right are positive while locations to the left are negative. For example, the fish in its initial location has left and right neighbors at locations -1 and +1, respectively. What are the possible positions of the fish, relative to its starting position, at the end of a simulation with six coin flips? Explain. (Don't worry about the tank being too short.)
• Conduct several such simulations, each with six coin flips as described in the exercise above, and keep track of where the fish ends up after each simulation. Is the fish more likely to end up in one position than another? Explain why or why not.

In this lab you will write a program that will simulate a fish (or other object) moving randomly back and forth six times, starting at location 0. Initially your program will print the final location of the object (an integer between -6 and 6). You will then modify your program to run the simulation 1000 times, keeping track of how many times the object ends up in each of the possible final locations. Finally, you will enhance your program to draw a histogram (bar graph) of the various final locations. For example, a text-based histogram might look like the following:

```     -6   xxxxx
-4   xxxxxxxxx
-2   xxxxxxxxxxxxxx
0   xxxxxxxxxxxxxxxxx
2   xxxxxxxxxxxxxx
4   xxxxxxxxx
6   xxxxx
```

Simulate an object moving six times.

• Download and unzip the HistogramUsingBlueJ zip file. Open it in BlueJ as a non-BlueJ project (BlueJ will turn it into a BlueJ project).
• All of your modifications for this lab will be in `HistogramApp.java`. In fact, for the first two parts of the lab, all of your modifications will be where there is a comment saying "YOUR CODE GOES HERE!" For now, you can ignore the commented-out code to construct a grid and add blocks. As you modify the file, remember to write appropriate comments that describe the purpose of the code you are about to write (what you are trying to accomplish) before writing it.
• Research the class documentation (not the code!) for the `Coin` class to learn how to construct a coin using the default `Coin` constructor. Also create a local integer variable to represent the object's location (-6 through 6). Initialize it to 0.
• Research the `Coin` class documentation to learn how to toss a coin and how to determine whether the tossed coin is showing heads or tails. Modify your program to toss the coin six times. Each time you toss the coin, update the location variable to reflect a move to the right if the coin comes up heads, or a move to the left if the coin comes up tails.
• After moving the object six times, print its final location. Run your program several times to test it.

• In order to keep track of how many times the object ends up in each location over the course of many runs of the simulation, create a local integer variable to represent each of the possible final locations, e.g., ```minusSixCount, zeroCount,``` etc. Be sure to initialize each of them to 0. (Question: how many integer variables will you need to represent all of the possible final locations?)
• Embed the code you wrote earlier in a loop, in order to run the simulation 1000 times. Remember that the fish needs to start at location 0 each time. Rather than printing out 1000 final location values, just increment the appropriate location counter after each run. (Note that there's a constant called `NUM_ITERATIONS` that you should use instead of "hard-coding" the number 1000 throughout your code.)
• After running the simulation `NUM_ITERATIONS` times, print the number of times the fish (or other object) ended up in each of the possible final locations. Run your program several times to test it. Do your results seem to make sense? You may wish to double-check that the various counts add up to 1000.

Draw a histogram.

• Examine the commented-out block of code that constructs a `SimpleGrid` object to display a histogram. The first two statements create the grid. This grid is 8 rows and `NUM_ITERATIONS` columns long. (Question: Why 8 rows? Why `NUM_ITERATIONS` columns?) The next few statements place the string "-6" in cell (0, 2) and a red block in cell (0, 5). The last statement displays the grid. Note that a block can only be added to a single location in the grid; you will need a new block for each location. Uncomment this code, run the program, and see what happens.
• Research the `TextCell` class documentation to review how to create a text cell. Research the `ColorBlock` class documentation to review how to create a color block. Next, research the `SimpleGrid` class documentation to review how to call the `add` method. (Follow this link rather than looking at the `SimpleGrid` documentation via BlueJ; this version of the class documenation has been stripped of some unnecessary complexity.)
• Update the strings in the statement that creates the `SimpleGridObjectGUI` object to reflect your name instead of "John Doe", update the date, and add to the second string the names of anyone who helped you with this lab. Compile and run the program again to check out that you made the correct changes. The information you edited can be seen by selecting the help menu item and the "About SimpleGridObject..." command.
• Now you are ready to make a histogram of your results. To do this, you will place color blocks in rows, where each row will indicate the number of times the fish ended up in a particular final location. First, we will create just a single row indicating how many times the fish ended up in the -6 location. For example, if the fish ended up in the -6 location 12 times, you will create 12 color blocks and place them in the first row, each in a different column. Test your program with your partial (one row) histogram.
• Once you have the first row of your histogram displaying correctly, add in the code to fill in the remaining rows of the histogram.
• Run your program several times to test it. You may also want to experiment with running it different numbers of times; for example, run the program several times with `NUM_ITERATIONS` set to 10, then several times with `NUM_ITERATIONS` set to 20 and 100. How does the behavior change as the number of iterations changes? Why?
• When you're sure that your program runs correctly, you may remove the System.out.println statements that printed the various final location counters. Also remove the statement at the beginning of the `main` method that said what the program would do once it was written.

• Update the class documentation for `HistogramApp.java` to accurately describe the purpose and behavior of the class from a user's perspective.  Focus on what the program does, rather than how it does it. Include your name and the date as well as the names of anyone from whom you received help. Providing proper documentation is an important step towards writing well-structured and reusable programs.
• When you are done and your program works correctly, submit your HistogramApp.java file.

If you have time...

If there is still time left in lab when you are done, you may choose one or more of the following exercises to implement. They can be done in any order and you may start with any of them.

• Modify the code so that the number of columns in the grid is only as large as the largest bar in the histogram.
• Modify the code to scale the size of the bars by allowing each grid square to represent 10 coin tosses instead of one (or pick another scaling factor).
• Use vertical bars for your histogram instead of horizontal bars. Remember that (0, 0) is the upper left of the grid.

After you have completed this lab and the More Fish! mini-lab, you can start work on Programming Project #3 (due at the beginning of Lab 4).

+These questions came from the Advanced Placement Computer Science Marine Biology Simulation Case Study, available from the College Board.