Representing Fields

Our simulation is starting to come together, we have classes that we can use to create instances of:

  • Wheat
  • Potato
  • Cow
  • Sheep

This means that we can simulate these individually but we need to move further towards our goal of being able to simulate the entire farm. One step towards this is to be able to group animals and crops together in fields that can then be simulated together based on the weather conditions over that field and the amount of food that is available on any particular day.

Class diagram

The class diagram for the field class is as follows:

Field Class Diagram

Notice, that in our field we will be able to group together both crops and animals and for now we are going to assume this is okay - the animals are very well behaved and won't just eat the crops!

Creating the field class

The video below demonstrates how to create the field class:


Task 9

Use the above video to help you create a field class.


Adding methods to our field class

Now that we have the basic structure of our field class we need to turn our attention to the methods that we will require. Most important are those methods that enable us to add and remove animals from a field instance.

The updated class diagram would be:

Updated class diagram

The video below demonstrates how to add these methods:


Task 10

Use the above video to help you add methods to enable you to add crops and animals to the field and also to remove them.


Testing the field

Before we consider any other methods that are required we are going to test what we have. To do this we will create functions that can be used to:

  • tell us what crops and animals are in the field
  • allow us to select and remove a particular crop or animal

The video below demonstrates how to do this:


Task 11

Use the above video to help you add the necessary functions to test the field class.


Further methods

In the next section we will look to improve our field class by adding further methods to report on the contents of the field and to grow whatever is in the field.