Animal
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 }