Difference between revisions of "Animal"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==The Code== <syntaxhighlight lang="csharp" line> class Animal { protected double NaturalLifespan; protected int ID; protected static int NextID = 1; prot...")
 
Line 1: Line 1:
 
==The Code==
 
==The Code==
 
+
<tabber>
 +
C#=
 
<syntaxhighlight lang="csharp" line>
 
<syntaxhighlight lang="csharp" line>
 
   class Animal
 
   class Animal
Line 63: Line 64:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
|-|
 +
VB=
 +
<syntaxhighlight lang="vbnet" line>
  
 +
</syntaxhighlight>
 +
</tabber>
 
=Explanation=
 
=Explanation=

Revision as of 23:41, 30 November 2016

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