Lab 8: Object-Oriented Programming
Due at 11:59:59 pm on Thursday, 10/28/2021.
Starter Files
Download lab08.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the OK autograder.
Submission
By the end of this lab, you should have submitted the lab with python3 ok --submit
. You may submit more than once before the deadline; only the final submission will be graded. Check that you have successfully submitted your code on okpy.org. See this article for more instructions on okpy and submitting assignments.
- Submit the
lab08.py
file took
.
OOP terminology
Object-oriented programming (OOP) is a style of programming that
allows you to think of code in terms of "objects." Here's an example of
a Car
class:
class Car(object):
num_wheels = 4
gas = 30
headlights = 2
size = 'Tiny'
def __init__(self, make, model):
self.make = make
self.model = model
self.color = 'No color yet. You need to paint me.'
self.wheels = Car.num_wheels
self.gas = Car.gas
def paint(self, color):
self.color = color
return self.make + ' ' + self.model + ' is now ' + color
def drive(self):
if self.wheels < Car.num_wheels or self.gas <= 0:
return 'Cannot drive!'
self.gas -= 10
return self.make + ' ' + self.model + ' goes vroom!'
def pop_tire(self):
if self.wheels > 0:
self.wheels -= 1
def fill_gas(self):
self.gas += 20
return 'Gas level: ' + str(self.gas)
Here's some terminology:
- class: a blueprint for how to build a certain type of object.
The
Car
class (shown above) describes the behavior and data that allCar
objects have. instance: a particular occurrence of a class. In Python, we create instances of a class like this:
>>> my_car = Car('Tesla', 'Model S')
my_car
is an instance of theCar
class.attribute or field: a variable that belongs to the class. Think of an attribute as a quality of the object: cars have wheels and size, so we have given our
Car
classself.wheels
andself.size
attributes. We can access attributes using dot notation:>>> my_car.size 'Tiny' >>> my_car.wheels 4
method: Methods are just like normal functions, except that they are tied to an instance or a class. Think of a method as a "verb" of the class: cars can drive and also pop their tires, so we have given our
Car
class the methodsdrive
andpop_tire
. We call methods using dot notation:>>> my_car = Car('Tesla', 'Model S') >>> my_car.drive() 'Tesla Model S goes vroom!'
constructor: As with data abstraction, constructors describe how to build an instance of the class. Most classes have a constructor. In Python, the constructor of the class defined as
__init__
. For example, here is theCar
class's constructor:def __init__(self, make, model): self.make = make self.model = model self.color = 'No color yet. You need to paint me.' self.wheels = Car.num_wheels self.gas = Car.gas
The constructor takes in two arguments,
make
andmodel
. As you can see, the constructor also creates theself.color
,self.wheels
andself.gas
attributes.self
: in Python,self
is the first parameter for many methods (in this class, we will only use methods whose first parameter isself
). When a method is called,self
is bound to an instance of the class. For example:>>> my_car = Car('Tesla', 'Model S') >>> my_car.drive()
Notice that the
drive
method takes inself
as an argument, but it looks like we didn't pass one in! This is because the dot notation implicitly passes incar
asself
for us.
Car WWPD
Question 1: Car
Use OK to test your knowledge with the following What would Python print questions:
python3 ok -q car -u
If you get stuck try typing these in the interpreter yourself
python3 -i
Keyboard
Question 2: Keyboard
We'd like to create a Keyboard
class that takes in an arbitrary
number of Button
s and stores these Button
s in a dictionary. The
keys in the dictionary will be strings that represent the position on the
Keyboard
, and the values will be the respective Button
. Fill out
the methods in the Keyboard
class according to each description,
using the doctests as a reference for the behavior of a Keyboard
.
class Keyboard:
"""A Keyboard takes in a list of buttons, and has a
dictionary of positions as keys, and Buttons as values.
>>> b1 = Button("button1", "H")
>>> b2 = Button("button2", "I")
>>> k = Keyboard([b1, b2])
>>> "button1" in k.buttons.keys() # Make sure to add the button to dictionary
True
>>> k.buttons["button1"].letter
'H'
>>> k.buttons["button1"].name
'button1'
>>> k.press("button1")
'H'
>>> k.press("button100")
''
>>> b1.pressed
1
>>> b2.pressed
0
>>> k.typing(["button1", "button2"])
'HI'
>>> k.typing(["button2", "button1"])
'IH'
>>> b1.pressed # make sure typing calls press!
3
>>> b2.pressed
2
"""
def __init__(self, buttons):
self.buttons = {}
"*** YOUR CODE HERE ***"
for button in buttons:
self.buttons[button.name] = button
def press(self, name):
"""Takes in a position of the button pressed, and
returns that button's output. Return an empty string
if the button does not exist. You can access the keys
of a dictionary d with d.keys(). """
"*** YOUR CODE HERE ***"
if name in self.buttons.keys():
b = self.buttons[name]
b.pressed += 1
return b.letter
return ''
def typing(self, typing_input):
"""Takes in a list of buttons to be pressed, and
returns the total output. Make sure to call self.press"""
"*** YOUR CODE HERE ***"
accumulate = ''
for name in typing_input:
accumulate+=self.press(name)
return accumulate
class Button:
def __init__(self, name, letter):
self.name = name
self.letter = letter
self.pressed = 0
Use OK to test your code:
python3 ok -q Keyboard
Quidditch [Optional]
Note, this problem is optional this week as it covers inheritance!
Question 3: Quidditch
It's time for the opening quidditch match of the season! We represent the various positions for players with the QuidditchPlayer
class and its subclasses. Every player begins with a base_energy
level, but every position requires a different proportion of energy. Fill in the energy
method for the Beater
, Chaser
, Seeker
, and Keeper
classes, according to their docstrings. In addition, fill in the __init__
method for the Chaser
class.
class Player:
def __init__(self, name, base_energy):
"""
Players have a name, and begin with base_energy.
"""
self.name = name
self.base_energy = base_energy
def energy(self):
return self.base_energy
class Beater(QuidditchPlayer):
role = "bludgers"
def energy(self, time):
"""
Returns the amount of energy left after playing for time minutes.
After playing for time minutes, Beaters lose their base energy level
divided by the number of minutes. If time is 0, catch the ZeroDivisionError
and print "You can't divide by zero!" instead.
>>> fred = Beater("Fred Weasley", 640)
>>> fred.energy(40)
624.0
>>> fred.energy(0)
You can't divide by zero!
"""
"*** YOUR CODE HERE ***"
try:
return self.base_energy - (self.base_energy / time)
except ZeroDivisionError as e:
print("You can't divide by zero!")
Use OK to test your code:
python3 ok -q Beater.energy
class Chaser(QuidditchPlayer):
role = "score"
energy_expended = 20
def __init__(self, name, base_energy, goals):
"""
Chasers have a name, score goals, and begin with base_energy.
"""
"*** YOUR CODE HERE ***"
self.name = name
self.base_energy = base_energy
self.goals = goals
def energy(self, time):
"""
Returns the amount of energy left after playing for time minutes. For every goal
they score, they use energy_expended units of energy. In addition, they also use
10% of energy_expended if the number of minutes they have played is a multiple of 9.
>>> katie = Chaser("Katie Bell", 230, 2)
>>> katie.energy(20)
190
>>> ginny = Chaser("Ginny Weasley", 400, 3)
>>> ginny.energy(45)
338.0
"""
"*** YOUR CODE HERE ***"
energy = self.base_energy
if time % 9 == 0:
energy = energy - (0.1 * Chaser.energy_expended)
energy = energy - (self.goals * Chaser.energy_expended)
else:
energy = energy - (self.goals * Chaser.energy_expended)
return energy
Use OK to test your code:
python3 ok -q Chaser.energy
class Seeker(QuidditchPlayer):
role = "snitch"
energy_expended = 5
def energy(self, time):
"""
Returns the amount of energy after time minutes. Seekers expend energy_expended
units of their energy for every minute they have been playing.
>>> harry = Seeker("Harry Potter", 700)
>>> harry.energy(30)
550
"""
"*** YOUR CODE HERE ***"
return self.base_energy - (time * Seeker.energy_expended)
Use OK to test your code:
python3 ok -q Seeker.energy
class Keeper(QuidditchPlayer):
role = "guard"
energy_expended = 50
def energy(self, time):
"""
Returns the amount of energy after time minutes. If less than 30 minutes have
passed, then Keepers do not lose any energy. If 30 minutes or more have passed,
then Keepers expend 80% of their energy_expended units for every full 15
minutes that pass.
>>> oliver = Keeper("Oliver Wood", 380)
>>> oliver.energy(45)
260.0
"""
"*** YOUR CODE HERE ***"
energy = self.base_energy
if time < 30:
return self.base_energy
else:
for i in range(time // 15):
energy = energy - (0.8 * Keeper.energy_expended)
return energy
Use OK to test your code:
python3 ok -q Keeper.energy
After you finish implementing the QuidditchPlayers, run the following command in your terminal to play the game:
python3 -i quidditch_game.py
Submit
Make sure to submit this assignment by running:
python3 ok --submit