CIS300 Spring 2002: Assignment 1

5 points; due Friday, September 6, at 10:00pm
Rabbits reproduce rapidly. For centuries, people wondered exactly how rapidly. The question was stated formally by the Italian mathematician, Leonardo of Pisa, also called Fibonacci, in 1202:

Someone placed a pair of rabbits in a certain place, enclosed on all sides by a wall, so as to find out how many pairs of rabbits will be born there in the course of one year, it being assumed that every month a pair of rabbits produces another pair, and that rabbits begin to bear young two months after their own birth.
(Note that Fibonacci assumes that a rabbit pair always gives birth to a male-female pair.)

How many rabbits will result in a year? Fibonacci calculated the answer and noted there was a pattern to the population growth. The pattern, called the Fibonacci sequence, arises often in biology and botany and is defined by this equation set:

rabbitPairsAtMonth(0) = 1
rabbitPairsAtMonth(1) = 1
rabbitPairsAtMonth(n) = rabbitPairsAtMonth(n-1) + rabbitPairsAtMonth(n-2),  when n>1
For example, the rabbits after 4 months are calculated as follows:
rabbitPairsAtMonth(4) = rabbitPairsAtMonth(3) + rabbitPairsAtMonth(2)
  =  (rabbitPairsAtMonth(2) + rabbitPairsAtMonth(1))  
          +  (rabbitPairsAtMonth(1) + rabbitPairsAtMonth(0))
  =  ( (rabbitPairsAtMonth(1) + rabbitPairsAtMonth(0)) + 1 ) + (1 + 1)
  = ((1 + 1) + 1)  +  2    =  5
(By the way, rabbitPairsAtMonth(12) = 233. A bit of history about Fibonacci can be found at http://www.austms.org/Modules/Fib)

Programming Assignment

Your assignment is to implement Fibonacci's experiment within the computer. We will alter his original problem by assuming that rabbits die after some finite number of months. For example, we might assume that a rabbit dies at four months of age; thus a rabbit pair generates 2 pairs of offspring during its lifetime. Here is the resulting simulation:

Input format

The program asks the user for two inputs: the lifespan of a rabbit, in months, and the number of months the simulation should execute:
  1. The program lives in a package you construct, named RabbitSim; its main method is located in class Start of the package. The Teaching Assistant will start your program by typing the command,
    java RabbitSim.Start
    
  2. When started, the program generates a Java input dialog (JoptionPane.showInputDialog --- see Chapter 4 of the CIS200 notes) that asks:
    +----------------------------------------+
    | Type rabbit lifespan (a positive int): |
    |
    |       OK        Cancel                 |
    +----------------------------------------+
    
  3. After the user inputs an integer, the program dislays a second dialog:
    +------------------------------------------+
    | Type simulation length (a positive int): |
    |
    |       OK        Cancel                   |
    +------------------------------------------+
    
    and the user types a second int.
At this point, the program conducts the rabbit-reproduction simulation.

Output format

The results of the simulation will be displayed in two locations: (i) within the command window, so that the user can see the simulation as it progresses; (ii) within a sequential text file, rabbit.log, which saves a copy of all the information printed in the command window. The file can be studied after the simulation terminates.

Here is an example log file for a simulation where a rabbit has a lifespan of 4 months and the simulation goes for 6 full months:

Month 0:
birth of Rabbit.RabbitPair@207402 at 0; pair count = 1
Month 1:
Month 2:
birth of Rabbit.RabbitPair@a04e24 at 2; pair count = 2
Month 3:
birth of Rabbit.RabbitPair@6a5a54 at 3; pair count = 3
Month 4:
birth of Rabbit.RabbitPair@504c4b at 4; pair count = 4
death of Rabbit.RabbitPair@207402 at 4; pair count = 3
Month 5:
birth of Rabbit.RabbitPair@1c0e07f at 5; pair count = 4
birth of Rabbit.RabbitPair@1d2ef45 at 5; pair count = 5
Month 6:
birth of Rabbit.RabbitPair@15aa73b at 6; pair count = 6
death of Rabbit.RabbitPair@a04e24 at 6; pair count = 5
birth of Rabbit.RabbitPair@991f7e at 6; pair count = 6
The log must list the births and deaths of all rabbit pairs and keep a running total of the count of living rabbit pairs. Notice that each rabbit pair must be given a name; the cryptic names shown above are generated by a built-in Java method used in the package described below.

package Rabbit

It is lots of fun (but takes lots of time) to write the classes that correctly generate rabbit pairs, but this week's assignment is meant to give you practice with packages, importations, and interfaces. So, I have simplified matters by writing a package Rabbit, which contains a class RabbitPair. The class constructs a new rabbit-pair object on demand, gives birth to other rabbit-pair objects, ages, and ``dies.'' (Important: although I have written package Rabbit, you must study and understand its contents; the knowledge will be helpful for solving later assignments.)

Although class RabbitPair can construct rabbit pairs, it does not know how to generate output, and it assumes that someone else (you) will write a class that can do output. The class you write must implement interface MessageBoard, which is a Java interface included within package Rabbit. Here is a picture of the completed application you must build, where your job is to write the components of package RabbitSim and connect them to package Rabbit:


You can download package Rabbit, either in pieces or as a single jar file, at http://www.cis.ksu.edu/~schmidt/300f02/Assign/Assn1

How to Organize the Packages

The two packages, Rabbit and RabbitSim, are placed within a folder named Assign1. Read the section named ``Java Packages'' in the lecture, ``Class Diagrams and Interfaces,'' for the technical details. (If you are a BlueJ user, it is critical that you also read the document at http://www.cis.ksu.edu/~schmidt/300f02/bluej.packages.html)

Build and test your work in the above configuration. Remember to insert javadoc-style commentary into your work. When you are satisfied with the results, execute javadoc to generate the web documentation.

What to Sumbit

You must use the automated submission page at http://www.cis.ksu.edu/~schmidt/300f02/Assign/submit.html to submit a jar-file of your work. Here's how:
  1. Ensure that you have created a folder, named Assign1, and have placed packages Rabbit and RabbitSim within it. Ensure that all files in both packages are compiled and are ready for immediate testing.
  2. Enter (open) the Assign1 folder. Use javadoc to generate the web documentation for the Library package you wrote: Execute javadoc -classpath . Library. (Or, use BlueJ to do this.)
  3. Leave (close) the Assign1 folder. Now, create a jar-file, Assign1.jar, from Assign1, by typing this command in a command window:
    jar c Assign1 > Assign1.jar
    
    (A jar-file is Java's version of a ``zip'' file; it holds one entire folder in a single file.) See the instructions at http://www.cis.ksu.edu/~schmidt/300f02/Assign/how.to.jar.html for more details.
  4. Use the submission page at http://www.cis.ksu.edu/~schmidt/300f02/Assign/submit.html to submit Assign1.jar. Submit your jar-file before the deadline. (Late submissions are not accepted for grading.)
Important: Since the purpose of this week's assignment is to employ packages, importation, and interfaces, you will receive credit for your program only if it is organized in the package structure described above.