Difference between revisions of "Reduce the reproductive rate of a rabbit with age"
(Created page with "==Set method== there is already a get method for the ReproductionRate, but none for the set: <syntaxhighlight lang=csharp line> public void GetReproductionRate(double value) ...") |
(→Set value by age) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
==Set method== | ==Set method== | ||
− | there is already a get method for the ReproductionRate, but none for the set: | + | there is already a get method for the ReproductionRate, but none for the set. Add the nethod below into the Rabbit class: |
<syntaxhighlight lang=csharp line> | <syntaxhighlight lang=csharp line> | ||
− | public void | + | public void SetReproductionRate(double value) |
{ | { | ||
ReproductionRate = value; | ReproductionRate = value; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | ==Set value by age== | ||
+ | The Warren class has an AgeRabbits method, after the code to calculate the age add the code to reduce the ReproductionRate: | ||
+ | |||
+ | <syntaxhighlight lang=csharp line> | ||
+ | private void AgeRabbits(bool ShowDetail) | ||
+ | { | ||
+ | int DeathCount = 0; | ||
+ | for (int r = 0; r < RabbitCount; r++) | ||
+ | { | ||
+ | Rabbits[r].CalculateNewAge(); | ||
+ | |||
+ | //Added this line | ||
+ | Rabbits[r].SetReproductionRate(Rabbits[r].GetReproductionRate() - 0.1); | ||
+ | |||
+ | if (Rabbits[r].CheckIfDead()) | ||
+ | { | ||
+ | Rabbits[r] = null; | ||
+ | DeathCount++; | ||
+ | } | ||
+ | } | ||
+ | CompressRabbitList(DeathCount); | ||
+ | if (ShowDetail) | ||
+ | { | ||
+ | Console.WriteLine(" " + DeathCount + " rabbits die of old age."); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Display value== | ||
+ | The inspect method of the Rabbit class already displays the ReproductionRate, so no more coding is required. |
Latest revision as of 21:54, 13 February 2017
Set method
there is already a get method for the ReproductionRate, but none for the set. Add the nethod below into the Rabbit class:
1 public void SetReproductionRate(double value)
2 {
3 ReproductionRate = value;
4 }
Set value by age
The Warren class has an AgeRabbits method, after the code to calculate the age add the code to reduce the ReproductionRate:
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
8 //Added this line
9 Rabbits[r].SetReproductionRate(Rabbits[r].GetReproductionRate() - 0.1);
10
11 if (Rabbits[r].CheckIfDead())
12 {
13 Rabbits[r] = null;
14 DeathCount++;
15 }
16 }
17 CompressRabbitList(DeathCount);
18 if (ShowDetail)
19 {
20 Console.WriteLine(" " + DeathCount + " rabbits die of old age.");
21 }
22 }
Display value
The inspect method of the Rabbit class already displays the ReproductionRate, so no more coding is required.