Rabbit (Sub Class of Animal)

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

The Code

 1  class Rabbit : Animal
 2   {
 3     enum Genders
 4     {
 5       Male,
 6       Female
 7     }
 8     private double ReproductionRate;
 9     private const double DefaultReproductionRate = 1.2;
10     private const int DefaultLifespan = 4;
11     private const double DefaultProbabilityDeathOtherCauses = 0.05;
12     private Genders Gender;
13 
14     public Rabbit(int Variability)
15         : base(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability)
16     {
17       ReproductionRate = DefaultReproductionRate * CalculateRandomValue(100, Variability) / 100;
18       if (Rnd.Next(0, 100) < 50)
19       {
20         Gender = Genders.Male;
21       }
22       else
23       {
24         Gender = Genders.Female;
25       }
26     }
27 
28     public Rabbit(int Variability, double ParentsReproductionRate)
29         : base(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability)
30     {
31       ReproductionRate = ParentsReproductionRate * CalculateRandomValue(100, Variability) / 100;
32       if (Rnd.Next(0, 100) < 50)
33       {
34         Gender = Genders.Male;
35       }
36       else
37       {
38         Gender = Genders.Female;
39       }
40     }
41 
42     public override void Inspect()
43     {
44       base.Inspect();
45       Console.Write("Rep rate " + Math.Round(ReproductionRate, 1) + " ");
46       Console.WriteLine("Gender " + Gender + " ");
47     }
48 
49     public bool IsFemale()
50     {
51       if (Gender == Genders.Female)
52       {
53         return true;
54       }
55       else
56       {
57         return false;
58       }
59     }
60 
61     public double GetReproductionRate()
62     {
63       return ReproductionRate;
64     }
65   }
66 }

Explanation