Add the code to allow foxes to have a gender

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Explanation

I have looked at the code for a rabbit and copied the code relating to gender over into the fox class.

Firstly you need to add the is to the start of the class:

1     enum Genders
2       {
3           Male,
4           Female
5       }
6 
7     private Genders Gender;

This creates the different enum options, Genders can either be Male or Female. The Gender of the object is created using private.

This is the method from the rabbit class to check if an object is female. Copy the entire method into the fox class:

 1     public bool IsFemale()
 2     {
 3         if (Gender == Genders.Female)
 4         {
 5             return true;
 6         }
 7         else
 8         {
 9             return false;
10         }
11     }

In order to assign a Gender to a fox the code below needs to be added to the fox constructor:

1       if (Rnd.Next(0, 100) < 50)
2       {
3           Gender = Genders.Male;
4       }
5       else
6       {
7           Gender = Genders.Female;
8       }

In the inspect method add this line to display the Gender of the fox:

1 Console.Write("Gender " + Gender + " ");

What Now??

To use the new Gender you can change how the fox is reproduced:

 1 public bool ReproduceThisPeriod()
 2     {
 3       const double ReproductionProbability = 0.60;
 4       if (Rnd.Next(0, 100) < ReproductionProbability * 100 && IsFemale())
 5       {
 6         return true;
 7       }
 8       else
 9       {
10         return false;
11       }
12     }

This has made it so only a female fox can reproduce. But I have increased the probability.