Teacher's Guide

Chapter 5 / Level 2: Method Overriding

Teaching Objective:

  • Students understands the dynamic using child classes to override properties in parent classes
  • Understand the uses and optimization benefits of child classes in object inheritance method overriding
  • Learns the class and object syntax when creating and using method overriding

Guide

Method Overring is when a child class overrides a function in the parent class and is the basis for this exercise. In this level you will be filling bottles with oil, labeling them and using one of them to cook.

# Parent Class
class oil_bottle:

  label = ""

  def label_bottle(self):
    await player.read()

# Child Class
class fill_bottle(oil_bottle):

  def fill_bottle(self):
    player.place("empty bottle")
    player.collect("oilbottle")

  def label_bottle(self):
    await player.write(self.label)

The classes will be outlined in the code editor, they are used for filling up the oil bottles, the method overriding comes into play with the child class function label_bottle overwriting the same function in the parent class.

There are four (4) different oils bottled at four (4) different colored carpets where the dark X marks are located. They are bottled as follows: olive_oil is bottled on the yellow carpet, avocado_oil is bottled on the green carpet, canola_oil is bottled on the blue carpet, and vegetable_oil is bottled on the red carpet.

To start off walk over and grab the empty bottles so they can be filled up with oil. Make your way to the dark X marks, create objects for filling the bottles, set their labels and use their functions. Each of the labels should contain the capitalized name of each of the oils as follows: "Olive Oil" , "Avocado Oil" , "Canola Oil" and "Vegetable Oil" .

avocado_oil = fill_bottle()
avocado_oil.label = "Avocado Oil"
avocado_oil.label_bottle()
avocado_oil.fill_bottle()

# Do the same for the other oil bottles

Once the bottles are collected and filled, walk to the light X mark in front of the sack and use collect() function to grab "onion" .

player.collect("onion")

Create a variable named ingredients and add "Olive Oil" and "onion" to it. Walk to the gold X mark and use the place() function to cook the onion with the olive oil.

ingredients = ["Olive Oil", "onion"]
player.place(ingredients)

Lastly, in order to complete the level, put the rest of the oils you collected on a list named oils , walk to the light X mark by the purple carpet and use the place() function store the oils in the pantry.

oils = ["Avocado Oil" , "Canola Oil" , "Vegetable Oil"]
player.place(oils)

Map overview and path

Map overview and path

Code Solution

import player

class oil_bottle:
  
  label = ""
  
  async def label_bottle(self):
    await player.read()

 
class fill_bottle(oil_bottle):
  
  def fill_bottle(self):
    player.place("empty bottle")
    player.collect("oilbottle")
   
  async def label_bottle(self):
    await player.write(self.label)
    
    
player.move_forward(2)
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward(2)

avocado_oil = fill_bottle()
avocado_oil.label = "Avocado Oil"
avocado_oil.label_bottle()
avocado_oil.fill_bottle()

player.turn_left()
player.move_forward()
player.turn_right()

vegetable_oil = fill_bottle()
vegetable_oil.label = "Vegetable Oil"
vegetable_oil.label_bottle()
vegetable_oil.fill_bottle()

player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward(3)
player.turn_right()
player.move_forward(2)
player.turn_right()

olive_oil = fill_bottle()
olive_oil.label = "Olive Oil"
olive_oil.label_bottle()
olive_oil.fill_bottle()

player.turn_left()
player.move_forward()
player.turn_right()

canola_oil = fill_bottle()
canola_oil.label = "Canola Oil"
canola_oil.label_bottle()
canola_oil.fill_bottle()

player.turn_left()
player.turn_left()
player.move_forward()
player.turn_right()
player.collect("onion")
player.turn_left()
player.turn_left()
player.move_forward(4)

ingredients = ["Olive Oil", "onion"] 
player.place(ingredients)

player.turn_right()
player.move_forward(2)
player.turn_left()

oils = ["Avocado Oil" , "Canola Oil" , "Vegetable Oil"]
player.place(oils)
Chapter 5 / Level 2: Method Overriding | Teacher's Guide