Difference between revisions of "Fox (Sub Class of Animal)"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(AdvanceGeneration ;_;)
(Methods ;_;)
 
Line 122: Line 122:
 
But if the fox has eaten it checks to see if it's been killed by other causes except starvation
 
But if the fox has eaten it checks to see if it's been killed by other causes except starvation
 
However, if the fox has not died and is still alive the program will check whether the fox has eaten the number of food units needed and if it hasn't it will age a year.
 
However, if the fox has not died and is still alive the program will check whether the fox has eaten the number of food units needed and if it hasn't it will age a year.
 +
 +
===ResetFoodConsumed ;_;===
 +
This sets the number of food units consumed in the new period of time to 0.
 +
 +
===Reproduce ;_;===
 +
All foxes have a 25% each period of having a child. A random number between 0 and 100 is rolled, if a value below 25 is returned the fox has a child, if a value higher than 25 is returned the fox doesn't have a child.

Latest revision as of 15:54, 13 February 2017

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 ;_;

FoodUnitsNeeded ;_;

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 ;_;

This value is the amount of food eaten this period.

DefaultLifespan ;_;

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

DefaultProbabilityDeathOtherCauses ;_;

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

Methods ;_;

Fox ;_;

This method creates a fox using its base class Animal, with the lifespan and death from other causes probability of fox and Variability stated in the program.

AdvanceGeneration ;_;

Asks whether the fox has consumed 0 food units, if so they die. But if the fox has eaten it checks to see if it's been killed by other causes except starvation However, if the fox has not died and is still alive the program will check whether the fox has eaten the number of food units needed and if it hasn't it will age a year.

ResetFoodConsumed ;_;

This sets the number of food units consumed in the new period of time to 0.

Reproduce ;_;

All foxes have a 25% each period of having a child. A random number between 0 and 100 is rolled, if a value below 25 is returned the fox has a child, if a value higher than 25 is returned the fox doesn't have a child.