Homework 6
Due Tuesday 22-Feb, at 10:00pm
To start
- Create a folder named ‘hw6’
- Download hw6.py to that folder
- Edit hw6.py and modify the functions as required
- When you have completed and fully tested hw6, submit hw6.py to Gradescope.
For this hw, you may submit up to 15 times, but only your last submission counts.
Some important notes
- This homework is solo. You may not collaborate or discuss it with anyone outside of the course,
and your options for discussing with other students currently taking the course are limited.
See the academic honesty policy for more details.
- After you submit to Gradescope, make sure you check your score. If you aren’t sure
how to do this, then ask a CA or Professor.
- There is no partial credit on Gradescope testcases. Your Gradescope score is your Gradescope
score.
- Read the last bullet point again. Seriously, we won’t go back later and increase
your Gradescope score for any reason. Even if you worked really hard and it was only a
minor error…
- Do not hardcode the test cases in your solutions.
- The starter
hw6.py
file includes test functions to help you test on your own before
you submit to Gradescope. When you run your file, problems will be tested in order. If
you wish to temporarily bypass specific tests (say, because you have not yet completed
some functions), you can comment out individual test function calls at the bottom
of your file in main()
. However, be sure to uncomment and test everything together
before you submit! Ask a CA if you need help with this.
- Remember the course’s academic integrity policy. Solving the homework yourself is
your best preparation for exams and quizzes; cheating or short-cutting your learning
process in order to improve your homework score will actually hurt your course grade
long-term.
Limitations
Do not use sets, dictionaries, try/except, classes, or recursion this week.
The autograder (or a manual CA review later) will reject your submission entirely if you do.
A Note About Style Grading
Like in the previous assignment, we will be grading your code based on whether
it follows the 15-112 style guide. We
may deduct up to 10 points from your overall grade for style errors. We highly
recommend that you try to write clean code with good style all along, rather
than fixing your style issues at the end. Good style helps you code faster and
with fewer bugs. It is totally worth it. In any case, style grading already
started, so please use good style from now on!
A Note About Testing
You will notice that the skeleton file only includes testcases for some of the
functions you are writing. You should write testcases for the others. (You can
find some nice ways to test in the write-up below, but you will need to
translate those to actual testcases.)
Problems
-
Person Class [50 pts]
Write the class Person
so that the following test code passes:
def testPersonClass():
print('Testing Person Class...', end='')
fred = Person('fred', 32)
assert(isinstance(fred, Person))
assert(fred.getName() == 'fred')
assert(fred.getAge() == 32)
# Note: person.getFriends() returns a list of Person objects who
# are the friends of this person, listed in the order that
# they were added.
# Note: person.getFriendNames() returns a list of strings, the
# names of the friends of this person. This list is sorted!
assert(fred.getFriends() == [ ])
assert(fred.getFriendsNames() == [ ])
wilma = Person('wilma', 35)
assert(wilma.getName() == 'wilma')
assert(wilma.getAge() == 35)
assert(wilma.getFriends() == [ ])
wilma.addFriend(fred)
assert(wilma.getFriends() == [fred])
assert(wilma.getFriendsNames() == ['fred'])
assert(fred.getFriends() == []) # sadly, friends are not mutual!
assert(fred.getFriendsNames() == [])
wilma.addFriend(fred)
assert(wilma.getFriends() == [fred]) # don't add twice!
betty = Person('betty', 29)
fred.addFriend(betty)
assert(fred.getFriendsNames() == ['betty'])
pebbles = Person('pebbles', 4)
betty.addFriend(pebbles)
assert(betty.getFriendsNames() == ['pebbles'])
barney = Person('barney', 28)
barney.addFriend(pebbles)
barney.addFriend(betty)
assert(barney.getFriends() == [pebbles, betty])
assert(barney.getFriendsNames() == ['betty', 'pebbles']) # sorted!
fred.addFriend(wilma)
fred.addFriend(barney)
assert(fred.getFriends() == [betty, wilma, barney])
assert(fred.getFriendsNames() == ['barney', 'betty', 'wilma']) # sorted!
print('Passed!')
Note that your solution must work in general, and not hardcode to these specific test cases.
-
Bird Class and Subclasses [50 pts]
Write the Bird
, Penguin
, and MessengerBird
classes so that the following test code passes:
def getLocalMethods(clss):
import types
# This is a helper function for the test function below.
# It returns a sorted list of the names of the methods
# defined in a class. It's okay if you don't fully understand it!
result = [ ]
for var in clss.__dict__:
val = clss.__dict__[var]
if (isinstance(val, types.FunctionType)):
result.append(var)
return sorted(result)
def testBirdClasses():
print("Testing Bird classes...", end="")
# A basic Bird has a species name, can fly, and can lay eggs
bird1 = Bird("Parrot")
assert(type(bird1) == Bird)
assert(isinstance(bird1, Bird))
assert(bird1.fly() == "I can fly!")
assert(bird1.countEggs() == 0)
assert(str(bird1) == "Parrot has 0 eggs")
bird1.layEgg()
assert(bird1.countEggs() == 1)
assert(str(bird1) == "Parrot has 1 egg")
bird1.layEgg()
assert(bird1.countEggs() == 2)
assert(str(bird1) == "Parrot has 2 eggs")
assert(getLocalMethods(Bird) == ['__init__', '__repr__', 'countEggs',
'fly', 'layEgg'])
# A Penguin is a Bird that cannot fly, but can swim
bird2 = Penguin("Emperor Penguin")
assert(type(bird2) == Penguin)
assert(isinstance(bird2, Penguin))
assert(isinstance(bird2, Bird))
assert(bird2.fly() == "No flying for me.")
assert(bird2.swim() == "I can swim!")
bird2.layEgg()
assert(bird2.countEggs() == 1)
assert(str(bird2) == "Emperor Penguin has 1 egg")
assert(getLocalMethods(Penguin) == ['fly', 'swim'])
# A MessengerBird is a Bird that can optionally carry a message
bird3 = MessengerBird("War Pigeon", message="Top-Secret Message!")
assert(type(bird3) == MessengerBird)
assert(isinstance(bird3, MessengerBird))
assert(isinstance(bird3, Bird))
assert(not isinstance(bird3, Penguin))
assert(bird3.deliverMessage() == "Top-Secret Message!")
assert(str(bird3) == "War Pigeon has 0 eggs")
assert(bird3.fly() == "I can fly!")
bird4 = MessengerBird("Homing Pigeon")
assert(bird4.deliverMessage() == "")
bird4.layEgg()
assert(bird4.countEggs() == 1)
assert(getLocalMethods(MessengerBird) == ['__init__', 'deliverMessage'])
print("Done!")