Lab 7: Object-Oriented Programming
Due at 11:59:59 pm on 7/15/2024.
Starter Files
Download lab07.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the OK autograder.
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
Quidditch
Question 2: Quidditch
Quidditch is a sport from the Harry Potter series, played on flying broomsticks. In the game there are different roles and your job is to complete the classes for the given roles. We represent the various positions for players with the QuidditchPlayer
class and its subclasses.
class QuidditchPlayer:
def __init__(self, name, energy):
"""
QuidditchPlayers have a name and begin with some amount of energy.
"""
self.name = name
self.energy = energy
Beater
Implement the play
method of the Beater
class. After playing for time
minutes, Beaters lose N energy, where N is their current energy divided by the number of minutes.
If time
is 0, return "You can't divide by zero!"
. Otherwise, return "{name} played for {time} minutes"
. You may assume the energy level will never become negative.
class Beater(QuidditchPlayer):
def play(self, time):
"""
>>> fred = Beater("Fred Weasley", 640)
>>> fred.play(0)
"You can't divide by zero!"
>>> fred.play(40)
'Fred Weasley played for 40 minutes'
>>> fred.energy # Fred lost 640 / 40 energy points
624.0
>>> fred.play(10)
'Fred Weasley played for 10 minutes'
>>> fred.energy # Fred lost 624 / 10 energy points
561.6
"""
"*** YOUR CODE HERE ***"
if time == 0:
return "You can't divide by zero!"
else:
self.energy -= (self.energy / time)
return f"{self.name} played for {time} minutes"
Use OK to test your code:
python3 ok -q Beater.play
Chaser
Implement the __init__
and play
methods of Chaser
. Notice that Chaser
s have an additional instance attribute goals
which stores the number of goals they've scored.
For every goal scored, Chasers use energy_expended
units of energy. An additional 10% of energy_expended
is lost if the number of minutes played is a multiple of 9.
The play
method always returns "{name} played for {time} minutes"
. You may assume the energy level will never become negative.
class Chaser(QuidditchPlayer):
energy_expended = 20
def __init__(self, name, energy, goals):
"""
Chasers have a name, starting energy, and number of goals scored.
"""
"*** YOUR CODE HERE ***"
super().__init__(name, energy)
self.goals = goals
def play(self, time):
"""
>>> katie = Chaser("Katie Bell", 230, 2)
>>> katie.play(20)
'Katie Bell played for 20 minutes'
>>> katie.energy
190
>>> katie.play(10)
'Katie Bell played for 10 minutes'
>>> katie.energy
150
>>> ginny = Chaser("Ginny Weasley", 400, 3)
>>> ginny.play(45)
'Ginny Weasley played for 45 minutes'
>>> ginny.energy
338.0
"""
"*** YOUR CODE HERE ***"
self.energy -= self.goals * Chaser.energy_expended
if time % 9 == 0:
self.energy -= 0.1 * Chaser.energy_expended
return f"{self.name} played for {time} minutes"
Use OK to test your code:
python3 ok -q Chaser.play
Seeker
Implement the play
method of the Seeker
class. Seeker
s lose energy_expended
units of their energy for every minute they played.
The play
method always returns "{name} played for {time} minutes"
. You may assume the energy level will never become negative.
class Seeker(QuidditchPlayer):
energy_expended = 5
def play(self, time):
"""
>>> harry = Seeker("Harry Potter", 700)
>>> harry.play(30)
'Harry Potter played for 30 minutes'
>>> harry.energy
550
>>> harry.play(10)
'Harry Potter played for 10 minutes'
>>> harry.energy
500
"""
"*** YOUR CODE HERE ***"
self.energy -= time * Seeker.energy_expended
return f"{self.name} played for {time} minutes"
Use OK to test your code:
python3 ok -q Seeker.play
Keeper
Implement the play
method of the Keeper
class. If a Keeper
has played for 30 minutes or more, they lose 80% of their energy_expended
for every full 15 minutes that were played. Otherwise, they do not lose any energy.
The amount of time a Keeper
has played resets after each call to the play
method.
The play
method always returns "{name} played for {time} minutes"
. You may assume the energy level will never become negative.
class Keeper(QuidditchPlayer):
energy_expended = 50
def play(self, time):
"""
>>> oliver = Keeper("Oliver Wood", 380)
>>> oliver.play(45)
'Oliver Wood played for 45 minutes'
>>> oliver.energy
260.0
>>> oliver.play(10)
'Oliver Wood played for 10 minutes'
>>> oliver.energy
260.0
"""
"*** YOUR CODE HERE ***"
if time >= 30:
for i in range(time // 15):
self.energy -= 0.8 * Keeper.energy_expended
return f"{self.name} played for {time} minutes"
Use OK to test your code:
python3 ok -q Keeper.play
Submission
When you are done, submit your file to Gradescope. You only need to upload the following files:
lab07.py