Difference between revisions of "Reduce the reproductive rate of a rabbit with age"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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) ...")
 
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 GetReproductionRate(double value)
+
public void SetReproductionRate(double value)
 
{
 
{
 
ReproductionRate = value;
 
ReproductionRate = value;
 +
}
 +
</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() - .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:43, 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() - .05);
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 }