Wednesday 8 December 2010

Chapter 10 Test

Chapter Ten Test


Mr. Daly, I would like to state that Mike Silber has helped me a lot in this test and I was only able to complete some of the tasks thanks to his help. 
1. Open a clean version of "foxes-and-rabbits-v2".


DONE
Your task is to modify the project so that gender can be taken into account as animals breed "foxes-and-rabbits-v3".

2. To do this you should create an Interface Gender..


The Gender interface has been created



public interface Gender
{


  public boolean isMale(); 
  public boolean isFemale();
}



public interface Gender
{

  public boolean isMale();
  
  public boolean isFemale();
}


3. The Animal class should be modified to implement Interface Gender.


public abstract class Animal implements Gender


 In order to do this you will need to add a field and to modify the constructor.




 Do not change the parameter list of the constructor. Each time an Animal is constructed a random gender is assigned. The probability of male or female is equal.


THIS IS MY NEW ANIMAL CLASS:

import java.util.List;
import java.util.Random;


/**
 * A class representing shared characteristics of animals.
 * 
 * @author David J. Barnes and Michael Kolling
 * @version 2008.03.30
 */
public abstract class Animal implements Gender
{
    // Whether the animal is alive or not.
    private boolean alive;
    // The animal's field.
    private Field field;
    // The animal's position in the field.
    private Location location;
    
    private boolean gender;
    
    /**
     * Create a new animal at location in field.
     * 
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Animal(Field field, Location location)
    {
        alive = true;
        this.field = field;
        setLocation(location);
        Random Gender = new Random();
        gender = Gender.nextBoolean();
    }
    
    /**
     * Make this animal act - that is: make it do
     * whatever it wants/needs to do.
     * @param newAnimals A list to add newly born animals to.
     */
    abstract public void act(List<Animal> newAnimals);


    /**
     * Check whether the animal is alive or not.
     * @return true if the animal is still alive.
     */
    public boolean isAlive()
    {
        return alive;
    }


    /**
     * Indicate that the animal is no longer alive.
     * It is removed from the field.
     */
    public void setDead()
    {
        alive = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }


    /**
     * Return the animal's location.
     * @return The animal's location.
     */
    public Location getLocation()
    {
        return location;
    }
    
    
    
    /**
     * Return the animal's field.
     * @return The animal's field.
     */
    public Field getField()
    {
        return field;
    }
    
    /**
     * Place the animal at the new location in the given field.
     * @param newLocation The animal's new location.
     */
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }
    
    public boolean isMale()
    {
        return gender;
    }
    
    public boolean isFemale()
    {
        if(gender)
        return false;
    else return true;
    }
}





4. Test your implementation by creating some rabbits (male and female) and foxes (male and female), then checking that isMale() and isFemale() work. Does the frequency of males and females seem to be about 50% in the population.




THIS WORKS! 
The frequency is almost 50% since there are a little more males than females at the end.Show your code to the supervisor.


I was able to do this thanks to the Count class I created. Here is the code for it:



public class Count
{
   static int males;
   static int females;


   /**
    * Constructor for objects of class Count
    */
   public Count()
   {
       males = 0;
       females = 0;
   }


   static void count()
   {
       System.out.println(males + " :: " + females);
   }
   
   static void addMale()
   {
       males++;
   }
   
   static void addFemale()
   {
       females++;
   }
   
   static void removeMale()
   {
       males--;
   }
   
   static void removeFemale()
   {
       females--;
   }
}



The simulation should now work exactly as before because information regarding gender is not used.


5. Working only with class Rabbit: modify the canBreed() method so that it only allows a Rabbit to breed if it is next to another Rabbit of the opposite sex.

return ( (age >= BREEDING_AGE)  && foundOppositeSex);

hints....

foundOppositeSex is a local boolean variable initially set to false

foundOppositeSex is set to true if a Rabbit of the opposite sex is found in one of the adjacent locations in the field




6. Now run your simulation. How does the introduction of gender impact the changing populations? Make a quantitative comparison of V2 and V3.


7. Given what you have learned from V3, describe one way of extending the simulation in "V4". What hypothesis will you be testing with V4?


In v4, in order to create a more accurate simulation of the "circle of life" and the survival of the rabbits and foxes in nature, we should create a Disease class which randomly chooses either a fox or a rabbit and kills it due to disease. At the moment the simulation assumes that the rabbits only die because of old age or being eaten by a fox which is false, they may also die of diseases. The simulation also assumes that foxes only die due to hunger and old age, which is also not true. Just as any animal on earth they might die due to disease.


Also, we may want to include food for rabbits since at the moment, the simulation assumes that the rabbits can survive without food which is completely WRONG! Therefore, we might want to create an Herb or a Grass/ Vegetable class which is eaten by the rabbits.