Difference between revisions of "Make the probability of a rabbit dying increase with age, ie extra 5% per term"
(Created page with "The skeleton program has a method to get the reproduction rate but nothing to set it. The variable itself is also private, so a method is required: <syntaxhighlight lang="csha...") |
|||
Line 1: | Line 1: | ||
− | + | Within the rabbit class, add the following methods to set and get ProbalilityOfDeathOtherCauses: | |
− | <syntaxhighlight lang= | + | |
− | + | <syntaxhighlight lang=csharp line> | |
− | + | public double GetProbabilityOfDeathOtherCauses() | |
− | + | { | |
− | + | return ProbabilityOfDeathOtherCauses; | |
+ | } | ||
+ | |||
+ | public void setProbabilityOfDeathOtherCauses(double value) | ||
+ | { | ||
+ | ProbabilityOfDeathOtherCauses=value; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | the code below is contained within the Warren Class: | ||
+ | |||
+ | <syntaxhighlight lang=csharp line> | ||
+ | private void AgeRabbits(bool ShowDetail) | ||
+ | { | ||
+ | int DeathCount = 0; | ||
+ | for (int r = 0; r < RabbitCount; r++) | ||
+ | { | ||
+ | Rabbits[r].CalculateNewAge(); | ||
+ | // Added the line below | ||
+ | Rabbits[r].setProbabilityOfDeathOtherCauses(Rabbits[r].GetProbabilityOfDeathOtherCauses() + .05); | ||
+ | if (Rabbits[r].CheckIfDead()) | ||
+ | { | ||
+ | Rabbits[r] = null; | ||
+ | DeathCount++; | ||
+ | } | ||
+ | } | ||
+ | CompressRabbitList(DeathCount); | ||
+ | if (ShowDetail) | ||
+ | { | ||
+ | Console.WriteLine(" " + DeathCount + " rabbits die of old age."); | ||
+ | } | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 21:25, 13 February 2017
Within the rabbit class, add the following methods to set and get ProbalilityOfDeathOtherCauses:
1 public double GetProbabilityOfDeathOtherCauses()
2 {
3 return ProbabilityOfDeathOtherCauses;
4 }
5
6 public void setProbabilityOfDeathOtherCauses(double value)
7 {
8 ProbabilityOfDeathOtherCauses=value;
9 }
the code below is contained within the Warren Class:
1 private void AgeRabbits(bool ShowDetail)
2 {
3 int DeathCount = 0;
4 for (int r = 0; r < RabbitCount; r++)
5 {
6 Rabbits[r].CalculateNewAge();
7 // Added the line below
8 Rabbits[r].setProbabilityOfDeathOtherCauses(Rabbits[r].GetProbabilityOfDeathOtherCauses() + .05);
9 if (Rabbits[r].CheckIfDead())
10 {
11 Rabbits[r] = null;
12 DeathCount++;
13 }
14 }
15 CompressRabbitList(DeathCount);
16 if (ShowDetail)
17 {
18 Console.WriteLine(" " + DeathCount + " rabbits die of old age.");
19 }
20 }