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

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==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 clas...")
 
(Explanation)
Line 4: Line 4:
 
Firstly you need to add the is to the start of the class:
 
Firstly you need to add the is to the start of the class:
  
<syntaxhighlight lang="csharp">
+
<syntaxhighlight lang="csharp" line>
 
     enum Genders
 
     enum Genders
 
       {
 
       {
Line 10: Line 10:
 
           Female
 
           Female
 
       }
 
       }
 +
 
     private Genders Gender;
 
     private Genders Gender;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
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:
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
Line 27: Line 31:
 
     }
 
     }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
In order to assign a Gender to a fox the code below needs to be added to the fox constructor:
  
  
Line 40: Line 46:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
In the inspect method add this line to display the Gender of the fox:
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
Console.Write("Gender " + Gender + " ");
 
Console.Write("Gender " + Gender + " ");
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 15:40, 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:

    public bool IsFemale()
    {
        if (Gender == Genders.Female)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

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


      if (Rnd.Next(0, 100) < 50)
      {
          Gender = Genders.Male;
      }
      else
      {
          Gender = Genders.Female;
      }

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

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