Difference between revisions of "Add the code to allow foxes to have a gender"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Explanation)
(Explanation)
 
(8 intermediate revisions by 3 users not shown)
Line 18: Line 18:
 
This is the method from the rabbit class to check if an object is female. Copy the entire method into the fox class:
 
This is the method from the rabbit class to check if an object is female. Copy the entire method into the fox class:
  
<syntaxhighlight lang="csharp">
+
<syntaxhighlight lang="csharp" line>
 
     public bool IsFemale()
 
     public bool IsFemale()
 
     {
 
     {
Line 34: Line 34:
 
In order to assign a Gender to a fox the code below needs to be added to the fox constructor:
 
In order to assign a Gender to a fox the code below needs to be added to the fox constructor:
  
 
+
<syntaxhighlight lang="csharp" line>
<syntaxhighlight lang="csharp">
 
 
       if (Rnd.Next(0, 100) < 50)
 
       if (Rnd.Next(0, 100) < 50)
 
       {
 
       {
Line 47: Line 46:
  
 
In the inspect method add this line to display the Gender of the fox:
 
In the inspect method add this line to display the Gender of the fox:
<syntaxhighlight lang="csharp">
+
 
 +
<syntaxhighlight lang="csharp" line>
 
Console.Write("Gender " + Gender + " ");
 
Console.Write("Gender " + Gender + " ");
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
==What Now??==
 +
To use the new Gender you can change how the fox is reproduced:
 +
 +
<syntaxhighlight lang="csharp" line>
 +
public bool ReproduceThisPeriod()
 +
    {
 +
      const double ReproductionProbability = 0.60;
 +
      if (Rnd.Next(0, 100) < ReproductionProbability * 100 && IsFemale())
 +
      {
 +
        return true;
 +
      }
 +
      else
 +
      {
 +
        return false;
 +
      }
 +
    }
 +
</syntaxhighlight>
 +
 +
This has made it so only a female fox can reproduce. But I have increased the probability.

Latest revision as of 12:55, 23 May 2017

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.