Fox (Sub Class of Animal)

From TRCCompSci - AQA Computer Science
Revision as of 14:50, 6 February 2017 by Cameron (talk | contribs) (Explanation)
Jump to: navigation, search

The Code

 1   class Fox : Animal
 2   {
 3     private int FoodUnitsNeeded = 10;
 4     private int FoodUnitsConsumedThisPeriod = 0;
 5     private const int DefaultLifespan = 7;
 6     private const double DefaultProbabilityDeathOtherCauses = 0.1;
 7 
 8     public Fox(int Variability)
 9         : base(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability)
10     {
11       FoodUnitsNeeded = (int)(10 * base.CalculateRandomValue(100, Variability) / 100);
12     }
13 
14     public void AdvanceGeneration(bool ShowDetail)
15     {
16       if (FoodUnitsConsumedThisPeriod == 0)
17       {
18         IsAlive = false;
19         if (ShowDetail)
20         {
21           Console.WriteLine("  Fox dies as has eaten no food this period.");
22         }
23       }
24       else
25       {
26         if (CheckIfKilledByOtherFactor())
27         {
28           IsAlive = false;
29           if (ShowDetail)
30           {
31             Console.WriteLine("  Fox killed by other factor.");
32           }
33         }
34         else
35         {
36           if (FoodUnitsConsumedThisPeriod < FoodUnitsNeeded)
37           {
38             CalculateNewAge();
39             if (ShowDetail)
40             {
41               Console.WriteLine("  Fox ages further due to lack of food.");
42             }
43           }
44           CalculateNewAge();
45           if (!IsAlive)
46           {
47             if (ShowDetail)
48             {
49               Console.WriteLine("  Fox has died of old age.");
50             }
51           }
52         }
53       }
54     }
55 
56     public void ResetFoodConsumed()
57     {
58       FoodUnitsConsumedThisPeriod = 0;
59     }
60 
61     public bool ReproduceThisPeriod()
62     {
63       const double ReproductionProbability = 0.25;
64       if (Rnd.Next(0, 100) < ReproductionProbability * 100)
65       {
66         return true;
67       }
68       else
69       {
70         return false;
71       }
72     }
73 
74     public void GiveFood(int FoodUnits)
75     {
76       FoodUnitsConsumedThisPeriod = FoodUnitsConsumedThisPeriod + FoodUnits;
77     }
78 
79     public override void Inspect()
80     {
81       base.Inspect();
82       Console.Write("Food needed " + FoodUnitsNeeded + " ");
83       Console.Write("Food eaten " + FoodUnitsConsumedThisPeriod + " ");
84       Console.WriteLine();
85     }
86   }

Explanation

Attributes :D

FoodUnitsNeeded :D

The amount of food needed per period, but this current value is just the default and can change based on the formula in the animal class.

FoodUnitsConsumedThisPeriod :D

This value is the amount of food eaten this period.

DefaultLifespan :D

The average lifespan of the Animal sub class Fox but of course there is some variance.

DefaultProbabilityDeathOtherCauses :D

The default chance that the fox will die to causes other than starvation or old age.

Methods