Sunday, March 18, 2007

simulating a rabbit colony_with java

I'm posting the document that I find on net to you. It's very practical and explains the event step by step .I think you can came out with more creative and illustrative stuff later after you acomplish that simulation example .. smile :)

We would like to write a program which models how a rabbit colony grows over time. We start with
a single male-female pair of baby rabbits. It takes two months for a baby to become an adult. At the
beginning of the month in which the pair reaches adulthood, they produce a pair of male-female baby
rabbits. They continue producing a single male-female pair of babies every month after that.
Data members of RabbitColony
Putting on the class provider hat, we start by deciding what data members are needed to describe a rabbit
colony. We will need three variables: one variable for storing the number of pairs of baby rabbits, one for
storing the number of pairs of rabbits that are 1 month old, and a third variable for storing the number
of pairs of adult rabbits. The beginning of our class would therefore look something like this:
class RabbitColony
{
int babies = 1; /* number of pairs of baby rabbits
(starting with 1) */
int young = 0; /* number of pairs of 1-month old rabbits */
int adults = 0; /* number of pairs of adult rabbits */
}
Methods of RabbitColony
We next decide what methods we need in our class, i.e. what would the class user want to do with a
rabbit colony? We decided on the following methods:

• a method which grows the colony for 1 month,
• a method which grows the colony for any number of months, and
• a method which returns the total number of rabbits in the colony.
This is the outline of the class with empty methods:
class RabbitColony
{
int babies = 1; /* number of pairs of baby rabbits
(starting with 1) */
int young = 0; /* number of pairs of 1-month old rabbits */
int adults = 0; /* number of pairs of adult rabbits */
// grow the rabbit colony by one month:
void grow()
{}
// grow the rabbit colony by n months:
void grow(int n)
{}
// calculate the total number of rabbits and return the value
int getNumRabbits()
{}
}
The first grow method takes no arguments and returns nothing. It will simply change the state of the
object (i.e. increase the variables in the class). The second grow method takes an integer as an argument
(the number of months to grow the colony). It also returns nothing, since it just updates the variables.
The getNumRabbits takes no arguments, but returns an integer.

• a method which grows the colony for 1 month,
• a method which grows the colony for any number of months, and
• a method which returns the total number of rabbits in the colony.
This is the outline of the class with empty methods:
class RabbitColony
{
int babies = 1; /* number of pairs of baby rabbits
(starting with 1) */
int young = 0; /* number of pairs of 1-month old rabbits */
int adults = 0; /* number of pairs of adult rabbits */
// grow the rabbit colony by one month:
void grow()
{}
// grow the rabbit colony by n months:
void grow(int n)
{}
// calculate the total number of rabbits and return the value
int getNumRabbits()
{}
}
The first grow method takes no arguments and returns nothing. It will simply change the state of the
object (i.e. increase the variables in the class). The second grow method takes an integer as an argument
(the number of months to grow the colony). It also returns nothing, since it just updates the variables.
The getNumRabbits takes no arguments, but returns an integer.
Using the RabbitColony class
Before we write the code inside the methods, we will take off the class provider hat and put on the class
user hat to see how this class could be used. This is an approach often used by programmers: to imagine
how your class will be used before you have finished writing it. It helps to ensure that the methods you
have chosen make sense. Here is a sample program that uses the RabbitColony class:
public class UseRabbitColony
{
public static void main(String[] args)
{
RabbitColony rc = new RabbitColony();
rc.grow(6); // grow rabbit colony for 6 months
int num = rc.getNumRabbits();
System.out.println("Number of rabbits after 6 months = " + num);
}
}
We will run this later, when we have finished writing the class. Note that we have used the default
constructor for creating the RabbitColony object. Although we have to provide code for all methods
in the class, the default constructor is an exception, since it is automatically defined if we do not write
our own constructor. In Section 3.10, we will write our own constructor.
Now that we have a better idea of how the class can be used, we can go ahead and write the methods.

Defining the grow() method
In the grow method, we have to simulate how the number of babies, young and adults increase in one
month. We have to convert the following facts into Java code:
• all the current young rabbits become adults;
• all the current babies become young;
• all the adult pairs (including the new ones) each produce a single baby pair.
Here is the Java code corresponding to the statements above:
// grow the rabbit colony by one month:
void grow()
{
adults += young; // all the young become adults
young = babies; // all the current babies become young
babies = adults; // all adult pairs produce a baby pair
}
Note that the order of these statements is significant. Can you see why? Do Exercise 3.10.
Defining the grow(int n) method
The parameterized grow method must grow the colony for any number of months. The first thing to
notice is the variable n. In the argument list of the method definition we have to indicate the type of the
argument (in this case an int). We also have to specify a name for the argument, so that we can refer
to it inside the method. We have chosen to call it n, but could have chosen any other suitable name.
In the main method, where we used the RabbitColony class, we grew the colony for 6 months
(rc.grow(6)), but we could have used any number as an argument. We call the number 6 an actual
parameter. In the method definition, we call the variable n a formal parameter.
Inside the method, all we do is call the grow() method n times. Here is the code:
// grow the rabbit colony for n months
void grow(int n)
{
for(int i=1; i<=n; i++)
grow();
}
Notice that we don’t need to precede the method call with the object name in this case, because we are
inside the class.
Defining the getNumRabbits() method
The getNumRabbits method must calculate the total number of rabbits and return the answer. Here
is the code:
// calculate the total number of rabbits and return the value
int getNumRabbits()
{
int num = 2*(babies + young + adults);
return num;
}

The first statement calculates the total number of rabbits, by adding the pairs and then multiplying by
two. This is stored in a variable called num. The Java keyword for producing an answer from a method
is return. In this way the method returns the value stored inside num.
Tracing UseRabbitColony
We will now explain how UseRabbitColony works, by tracing through the program. The first statement
is:
RabbitColony rc = new RabbitColony();
It creates the object in memory with the default values, as illustrated in Figure 3.7.
The second statement is:
rc.grow(6);
It results in a jump to the second grow method. Note: Java knows to use the second grow method
and not the first one, because we have given it an argument. In the method, n will take on the value
of 6 (only for the duration of this method call). The body of the loop will therefore execute 6 times.
Each time, the program jumps to the other grow method to execute the three statements there. Table 3.3
shows how the variables change on each loop iteration. See if you can work them out for yourself.
The third statement of the program is:
int numR = rc.getNumRabbits();
Java will first evaluate the right hand side. To do so, it has to jump to the getNumRabbits method.
At this stage, babies contains 5, young contains 3 and adults contains 5. These values are totalled
and multiplied by 2 to produce an answer of 26, which is returned to the calling statement and stored in
numR. Finally, this value is printed out.

No comments: