C#: Each year in the spring the carrots grow, with each carrot plant producing s
ID: 3813228 • Letter: C
Question
C#:
Each year in the spring the carrots grow, with each carrot plant producing six new plants before the original plant dies. During the summet, the rabbits eat the carrots (each rabbit consumes one carrot plant) and then each pair of rabbits producing between five and ten offspring. Unlike the carrots, the adult rabbits will not die afterwards. Fibally, during the fall, the foxes will eat the rabbits before having 5heir own young. Each fox will eat five rabbits, and then each pair of foxes will have a litter of three kits. 10% of the foxes will die each winter due to rough weather.
If it is assumed that each aspect of the environment occurs independently (first the carrots grow, the rabbits eat the carrots after they've finished growing, the foxes eat the rabbits after they finish growing). Write three separate recursive functions that simulate the population changes in this ecosystem.
Explanation / Answer
using System;
public class Test
{
public int rabbitCount = 10;
public int carrotCount = 10;
public int foxCount = 2;
public void printEcosystem()
{
Console.WriteLine("rabbitCount = " + rabbitCount);
Console.WriteLine("carrotCount = " + carrotCount);
Console.WriteLine("foxCount = " + foxCount);
}
public int maxYear = 10;
public int currentYear = 1;
public void spring() {
if(currentYear > maxYear)
return;
Console.WriteLine("It's Spring!!");
printEcosystem();
carrotCount = carrotCount * 6;
printEcosystem();
Console.WriteLine("Spring is Over");
summer();
}
public void summer() {
Console.WriteLine("It's Summer!!");
printEcosystem();
carrotCount = carrotCount - rabbitCount;
int x = rabbitCount / 2;
Random rnd = new Random();
rabbitCount += x * rnd.Next(5, 10);
printEcosystem();
Console.WriteLine("Summer is Over");
winter();
}
public void winter() {
Console.WriteLine("It's Winter!!");
printEcosystem();
rabbitCount -= foxCount*5;
int x = foxCount / 2;
Random rnd = new Random();
foxCount += x * 3;
foxCount = (int)(foxCount * 0.9);
printEcosystem();
Console.WriteLine("Summer is Over");
currentYear++;
spring();
}
public static void Main()
{
var p = new Test();
p.spring();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.