Adding methods to our class

In the previous section you were introduced to the concept of an interface - a way of interacting an object in a predefined way. The example used was that of a television remote control, the interface it provides is a set of buttons that you can press to change channels and access other functionality. Behind the scenes much more is happening but you interact with the television through this interface. The same is true with any classes you define, you will write methods that you and others can use to interact with objects of that type.

Updated class diagram

We are going to add two methods to our class:

  • Needs
  • Report

The updated class diagram would look as follows:

Crop Class Diagram

Needs method

The needs() method is designed to return to us the amount of light and water that the crop requires to grow. This will be returned using the following format:

{'light need': 4, 'water need': 3}

Report method

The report() method will provide an overview of the current state of the crop:

  • What type of crop it is
  • What its current status is
  • How much growth has occurred
  • How many days the crop has grown

Again, these values will be returned in the format:

{'type': 'generic', 'status': 'mature', 'growth': 12, 'days growing': 22}


You will have noticed that the return values for each method described above use a syntax that we have not introduced so far. This syntax describes a dictionary, which shares similarities with a regular list but which has additional features that are useful to us.

If we were to return the values from needs() as a list it would look like this:

crop_needs = [4,3]

This is fine as long as we remember which one of the numbers represents the light need and which the water need. To print the values on screen we could write:


We have to remember that the index 0 refers to light and that index 1 refers to water. This is annoying as it is easy to get these values confused.

A dictionary works differently, rather than storing values based on a index value, they are stored in key:value pairs. This means that you define a key that can be used to find the associated value in a more memorable way.

The dictionary that the needs() method show return is:

{'light need': 4, 'water need': 3}

Therefore to access the values we would write:

print(crop_needs['light need'])
print(crop_needs['water need'])

Notice that this is much more memorable than a simple index value. In other languages dictionaries are called associative arrays or key value pairs and they are another tool that can be used to make your program more readable and easier to understand.

Adding needs() and report() to the crop class

The video below demonstrates how to add the needs() and report() methods to the crop class:

Task 3

Use the above video to help you add the needs() and report() methods to the crop class.

Growing the crop

We have added methods that enable us to find out the information we need to grow the crop and to find out the current status of the crop. In this section we will turn out attention to actually growing the crop. To do this we will add two further methods:

  • update_status()
  • grow()

The updated class diagram would be:

Update crop class diagram

Update status method

The update_status() method will provide a way to change the status attribute to an appropriate value depending on the current amount of growth (stored in the growth attribute).

The following statuses will be available:

Status Growth
Seed 0
Seedling >0
Young >5
Mature >10
Old >15

Grow method

The grow() method will be responsible for actually growing the crop, and will require two parameters:

  • current available light
  • current available water

These parameters represent the current weather conditions for a particular day. If the grow method received the necessary amounts of light and water it will grow the crop based on the growth rate attribute and also increment the number of days growing and update the status of the crop.

In pseudo-code:

METHOD grow(light: integer, water: integer)
    IF light >= light_need AND water >= water_need THEN
        growth <- growth + growth_rate
    END IF
    days_growing <- days_growing + 1
    CALL update_status()

Adding update_status() and grow() to the crop class

The video below demonstrates how to add the update_status() and grow()methods to the crop class:

Task 4

Use the above video to help you add the update_status() and grow() methods to the crop class.

Private methods

In the above video you were introduced to the concept of private methods - update_status() was prefixed with an underscore to become _update_status().

As with private attributes that purpose of this is to indicate to other programmers that they should not use this method directly - it is there solely for other methods to use.

The reasons why you might want to make a method private are similar to the reasons why you might want to make an attribute private. Please refer back to that section if you require further explanation.

Testing the crop class

Now that we have defined all of the attributes and methods that the crop class requires it is time to test our class and make sure it works correctly.