Difference between revisions of "Make the probability of a rabbit dying increase with age, ie extra 5% per term"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
==Get Set Methods==
 
Within the rabbit class, add the following methods to set and get ProbalilityOfDeathOtherCauses:
 
Within the rabbit class, add the following methods to set and get ProbalilityOfDeathOtherCauses:
  
 
<syntaxhighlight lang=csharp line>
 
<syntaxhighlight lang=csharp line>
public double GetProbabilityOfDeathOtherCauses()
+
public double GetProbabilityOfDeathOtherCauses()
{
+
{
return ProbabilityOfDeathOtherCauses;
+
return ProbabilityOfDeathOtherCauses;
}
+
}
 +
 
 +
public void setProbabilityOfDeathOtherCauses(double value)
 +
{
 +
ProbabilityOfDeathOtherCauses=value;
 +
}
 +
</syntaxhighlight>
 +
 
 +
==Alter with age==
 +
the code below is contained within the Warren Class, it is the part of the program responsible for ageing each rabbit. I have commented the line added below. 0.5 is 5%:
  
public void setProbabilityOfDeathOtherCauses(double value)
+
<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())
 
{
 
{
ProbabilityOfDeathOtherCauses=value;
+
Rabbits[r] = null;
 +
DeathCount++;
 
}
 
}
 +
}
 +
CompressRabbitList(DeathCount);
 +
if (ShowDetail)
 +
{
 +
Console.WriteLine("  " + DeathCount + " rabbits die of old age.");
 +
}
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
the code below is contained within the Warren Class:
+
==Displaying data==
 +
You need to add a line to the inspect method of the Rabbit class to display the value. You can therefore check that it works:
 +
 
  
 
<syntaxhighlight lang=csharp line>
 
<syntaxhighlight lang=csharp line>
private void AgeRabbits(bool ShowDetail)
+
public override void Inspect()
{
+
{
int DeathCount = 0;
+
base.Inspect();
for (int r = 0; r < RabbitCount; r++)
+
Console.Write("Rep rate " + Math.Round(ReproductionRate, 1) + " ");
{
+
        //Added this line
Rabbits[r].CalculateNewAge();
+
Console.WriteLine("PDBOC: " + GetProbabilityOfDeathOtherCauses());
                                // Added the line below
+
Console.WriteLine("Gender " + Gender + " ");
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>

Latest revision as of 21:47, 13 February 2017

Get Set Methods

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 }

Alter with age

the code below is contained within the Warren Class, it is the part of the program responsible for ageing each rabbit. I have commented the line added below. 0.5 is 5%:

 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 				
10                 if (Rabbits[r].CheckIfDead())
11 		{
12 			Rabbits[r] = null;
13 			DeathCount++;
14 		}
15 	}
16 	CompressRabbitList(DeathCount);
17 	if (ShowDetail)
18 	{
19 		Console.WriteLine("  " + DeathCount + " rabbits die of old age.");
20 	}
21 }

Displaying data

You need to add a line to the inspect method of the Rabbit class to display the value. You can therefore check that it works:


1 public override void Inspect()
2 {
3 	base.Inspect();
4 	Console.Write("Rep rate " + Math.Round(ReproductionRate, 1) + " ");
5         //Added this line
6 	Console.WriteLine("PDBOC: " + GetProbabilityOfDeathOtherCauses());
7 	Console.WriteLine("Gender " + Gender + " ");
8 }