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)
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 35: Line 35:
  
  
<syntaxhighlight lang="csharp">
+
<syntaxhighlight lang="csharp" line>
 
       if (Rnd.Next(0, 100) < 50)
 
       if (Rnd.Next(0, 100) < 50)
 
       {
 
       {
Line 47: Line 47:
  
 
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>

Revision as of 15:41, 13 February 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 + " ");