Difference between revisions of "Animal"
(→Explanation) |
(→Explanation) |
||
(One intermediate revision by the same user not shown) | |||
Line 82: | Line 82: | ||
Used to increment the ID assigned to each fox, so each fox is assigned a unique ID. | Used to increment the ID assigned to each fox, so each fox is assigned a unique ID. | ||
− | === | + | ===Age=== |
+ | Again, what it says on the tin. The age of the fox. | ||
+ | |||
+ | ===ProbabilityOfDeathOtherCauses=== | ||
+ | Similarly to AvgLifespan, AvgProbabilityOfDeathOtherCauses is also declared when an instance of the class is created and calculated using this value and a random variability. |
Latest revision as of 12:47, 7 February 2017
Contents
The Code
1 class Animal
2 {
3 protected double NaturalLifespan;
4 protected int ID;
5 protected static int NextID = 1;
6 protected int Age = 0;
7 protected double ProbabilityOfDeathOtherCauses;
8 protected bool IsAlive;
9 protected static Random Rnd = new Random();
10
11 public Animal(int AvgLifespan, double AvgProbabilityOfDeathOtherCauses, int Variability)
12 {
13 NaturalLifespan = AvgLifespan * CalculateRandomValue(100, Variability) / 100;
14 ProbabilityOfDeathOtherCauses = AvgProbabilityOfDeathOtherCauses * CalculateRandomValue(100, Variability) / 100;
15 IsAlive = true;
16 ID = NextID;
17 NextID++;
18 }
19
20 public virtual void CalculateNewAge()
21 {
22 Age++;
23 if (Age >= NaturalLifespan)
24 {
25 IsAlive = false;
26 }
27 }
28
29 public virtual bool CheckIfDead()
30 {
31 return !IsAlive;
32 }
33
34 public virtual void Inspect()
35 {
36 Console.Write(" ID " + ID + " ");
37 Console.Write("Age " + Age + " ");
38 Console.Write("LS " + NaturalLifespan + " ");
39 Console.Write("Pr dth " + Math.Round(ProbabilityOfDeathOtherCauses, 2) + " ");
40 }
41
42 public virtual bool CheckIfKilledByOtherFactor()
43 {
44 if (Rnd.Next(0, 100) < ProbabilityOfDeathOtherCauses * 100)
45 {
46 IsAlive = false;
47 return true;
48 }
49 else
50 {
51 return false;
52 }
53 }
54
55 protected virtual double CalculateRandomValue(int BaseValue, int Variability)
56 {
57 return BaseValue - (BaseValue * Variability / 100) + (BaseValue * Rnd.Next(0, (Variability * 2) + 1) / 100);
58 }
59 }
Explanation
The class Animal declares the following variables:
NaturalLifespan
This is calculated using the AvgLifespan declared when an instance of the class is created (see Instantiation). This determines the age at which the animal dies.
ID
What it says on the tin. The unique identifier for each fox.
NextID
Used to increment the ID assigned to each fox, so each fox is assigned a unique ID.
Age
Again, what it says on the tin. The age of the fox.
ProbabilityOfDeathOtherCauses
Similarly to AvgLifespan, AvgProbabilityOfDeathOtherCauses is also declared when an instance of the class is created and calculated using this value and a random variability.