Last week, two of the discussion questions were:+
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.
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.
Coinclass to learn how to construct a coin using the default
Coinconstructor. Also create a local integer variable to represent the object's location (-6 through 6). Initialize it to 0.
Coinclass 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.
Add multiple runs.
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?)
NUM_ITERATIONSthat you should use instead of "hard-coding" the number 1000 throughout your code.)
NUM_ITERATIONStimes, 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.
SimpleGridobject to display a histogram. The first two statements create the grid. This grid is 8 rows and
NUM_ITERATIONScolumns long. (Question: Why 8 rows? Why
NUM_ITERATIONScolumns?) 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.
TextCellclass documentation to review how to create a text cell. Research the
ColorBlockclass documentation to review how to create a color block. Next, research the
SimpleGridclass documentation to review how to call the
addmethod. (Follow this link rather than looking at the
SimpleGriddocumentation via BlueJ; this version of the class documenation has been stripped of some unnecessary complexity.)
SimpleGridObjectGUIobject 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.
NUM_ITERATIONSset to 10, then several times with
NUM_ITERATIONSset to 20 and 100. How does the behavior change as the number of iterations changes? Why?
mainmethod that said what the program would do once it was written.
Submit your modifications.
HistogramApp.javato 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.
If you have time...
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.