Homework 10
Due by 11/9/2018
Instructions
Download hw10.zip. Inside the archive, you will find starter files for the questions in this homework, along with a copy of the OK autograder.
Submission: When you are done, submit the homework by uploading the
hw10.py
file to okpy.org.
You may submit more than once before the deadline; only the
final submission will be scored.
Readings: This homework relies on following references:
Question 1: Person
Modify the following Person
class to add a repeat
method, which
repeats the last thing said. If nothing has been said yet, you can
have repeat
return a string of your choosing. See the doctests for
an example of its use.
Hint: you will have to modify other methods as well, not just the
repeat
method.
class Person(object):
"""Person class.
>>> steven = Person("Steven")
>>> steven.say("Hello")
'Hello'
>>> steven.repeat()
'Hello'
>>> steven.greet()
'Hello, my name is Steven'
>>> steven.repeat()
'Hello, my name is Steven'
>>> steven.ask("preserve abstraction barriers")
'Would you please preserve abstraction barriers'
>>> steven.repeat()
'Would you please preserve abstraction barriers'
"""
def __init__(self, name):
self.name = name
"*** YOUR CODE HERE ***"
def say(self, stuff):
"*** YOUR CODE HERE ***"
return stuff
def ask(self, stuff):
return self.say("Would you please " + stuff)
def greet(self):
return self.say("Hello, my name is " + self.name)
def repeat(self):
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q Person --local
Question 2: Mint
Complete the Mint
and Coin
classes so that the coins created by a mint have
the correct year and worth.
- Each
Mint
instance has ayear
stamp. Theupdate
method sets theyear
stamp to thecurrent_year
class attribute of theMint
class. - The
create
method takes a subclass ofCoin
and returns an instance of that class stamped with themint
's year (which may be different fromMint.current_year
if it has not been updated.) - A
Coin
'sworth
method returns thecents
value of the coin plus one extra cent for each year of age beyond 50. A coin's age can be determined by subtracting the coin's year from thecurrent_year
class attribute of theMint
class.
Use OK to test your code:
python3 ok -q Mint --local
class Mint:
"""A mint creates coins by stamping on years.
The update method sets the mint's stamp to Mint.current_year.
>>> mint = Mint()
>>> mint.year
2015
>>> dime = mint.create(Dime)
>>> dime.year
2015
>>> Mint.current_year = 2100 # Time passes
>>> nickel = mint.create(Nickel)
>>> nickel.year # The mint has not updated its stamp yet
2015
>>> nickel.worth() # 5 cents + (85 - 50 years)
40
>>> mint.update() # The mint's year is updated to 2100
>>> Mint.current_year = 2175 # More time passes
>>> mint.create(Dime).worth() # 10 cents + (75 - 50 years)
35
>>> Mint().create(Dime).worth() # A new mint has the current year
10
>>> dime.worth() # 10 cents + (160 - 50 years)
120
>>> Dime.cents = 20 # Upgrade all dimes!
>>> dime.worth() # 20 cents + (160 - 50 years)
130
"""
current_year = 2015
def __init__(self):
self.update()
def create(self, kind):
"*** YOUR CODE HERE ***"
def update(self):
"*** YOUR CODE HERE ***"
class Coin:
def __init__(self, year):
self.year = year
def worth(self):
"The worth is a coin's face value + 1 cent for each year over age 50."
"*** YOUR CODE HERE ***"
class Nickel(Coin):
cents = 5
class Dime(Coin):
cents = 10