Difference between revisions of "Create a giant warren which allows 200 rabbits"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(The SuperWarren Class)
(The SuperWarren Class)
Line 31: Line 31:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
A SuperWarren is going to allow upto 200 rabbits, so you need to declare a value for MaxRabbitsInWarren. This should be a constant integer just like in the Warren class:
 +
 +
<syntaxhighlight lang=csharp>
 +
private const int MaxRabbitsInWarren = 200;
 +
</syntaxhighlight>
 +
 +
This should allow SuperWarren's to be created, but in-order to tell which are SuperWarren's and which are just normal ones we need to create an Inspect method:
 +
 +
<syntaxhighlight lang=csharp>
 +
      public override void Inspect()
 +
      {
 +
          base.Inspect();
 +
          Console.WriteLine("Super Warren");
 +
      }
 +
</syntaxhighlight>
 +
 +
This will run the inherited method first (ie base.Inspect) and then add a message to show this is a SuperWarren.
 +
 +
==Using the SuperWarren Class==

Revision as of 10:02, 24 May 2017

Before you can use the Warren class as the parent of the SuperWarren class you may need to create another constructor class. You will need to create one which doesn't accept any values, the other 2 constructor must have parameters passed:

    public Warren()
    {
        this.Variability = 45;
        Rabbits = new Rabbit[MaxRabbitsInWarren];
        RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
        for (int r = 0; r < RabbitCount; r++)
        {
            Rabbits[r] = new Rabbit(Variability);
        }
    }

Because we want to override the inspect method in the new class, you need to change the Inspect method to virtual:

    public virtual void Inspect()
    {
      Console.WriteLine("Periods Run " + PeriodsRun + " Size " + RabbitCount);
    }

The SuperWarren Class

Now we are ready to declare the SuperWarren class, find the end of the Warren class and declare:

class SuperWarren : Warren
{

}

A SuperWarren is going to allow upto 200 rabbits, so you need to declare a value for MaxRabbitsInWarren. This should be a constant integer just like in the Warren class:

private const int MaxRabbitsInWarren = 200;

This should allow SuperWarren's to be created, but in-order to tell which are SuperWarren's and which are just normal ones we need to create an Inspect method:

      public override void Inspect()
      {
          base.Inspect();
          Console.WriteLine("Super Warren");
      }

This will run the inherited method first (ie base.Inspect) and then add a message to show this is a SuperWarren.

Using the SuperWarren Class