|
## Object-Oriented Programming ##
# Q2
class Person(object):
"""Person class.
>>> steven = Person("Steven")
>>> steven.repeat() # starts at whatever value you'd like
'I squirreled it away before it could catch on fire.'
>>> 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
self.saved = 'I squirreled it away before it could catch on fire.'
"*** YOUR CODE HERE ***"
def say(self, stuff):
"*** YOUR CODE HERE ***"
self.saved = stuff
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 ***"
return self.saved
# Q3
class Account(object):
"""A bank account that allows deposits and withdrawals.
>>> eric_account = Account('Eric')
>>> eric_account.deposit(1000000) # depositing my paycheck for the week
1000000
>>> eric_account.transactions
[('deposit', 1000000)]
>>> eric_account.withdraw(100) # buying dinner
999900
>>> eric_account.transactions
[('deposit', 1000000), ('withdraw', 100)]
"""
interest = 0.02
def __init__(self, account_holder):
self.balance = 0
self.holder = account_holder
self.transactions = []
"*** YOUR CODE HERE ***"
def deposit(self, amount):
"""Increase the account balance by amount and return the
new balance.
"""
"*** YOUR CODE HERE ***"
self.balance = self.balance + amount
self.transactions += [('deposit', amount)]
return self.balance
def withdraw(self, amount):
"""Decrease the account balance by amount and return the
new balance.
"""
"*** YOUR CODE HERE ***"
if amount > self.balance:
return 'Insufficient funds'
self.balance = self.balance - amount
self.transactions += [('withdraw', amount)]
return self.balance
# Q4
class BadBankAccount(Account):
""" A subclass of bank account that allows an account holder to overdraw
once, and then prevents them from withdrawing more money. You should also
implement the property method overdrawn, which allows an account holder to
check if they are overdrawn.
>>> harold_account = BadBankAccount('Harold')
>>> harold_account.deposit(100) # depositing my paycheck for the week
100
>>> harold_account.withdraw(101) # buying dinner
-1
>>> harold_account.overdrawn
True
>>> harold_account.withdraw(100000)
You have overdrawn, please add more money!
-1
>>> harold_account.deposit(10)
9
>>> harold_account.overdrawn
False
"""
def withdraw(self, amount):
"""Decrease the account balance by amount and return the
new balance.
"""
"*** YOUR CODE HERE ***"
if self.balance < 0:
print ('You have overdrawn, please add more money!')
return self.balance
self.balance = self.balance - amount
return self.balance
"*** YOUR CODE HERE ***"
@property
def overdrawn(self):
return self.balance < 0
## Extra Object-Oriented Programming questions ##
from lab06 import *
# Q7
class CheckingAccount(Account):
"""A bank account that charges for withdrawals.
>>> check = Check("Steven", 42) # 42 dollars, payable to Steven
>>> steven_account = CheckingAccount("Steven")
>>> eric_account = CheckingAccount("Eric")
>>> eric_account.deposit_check(check) # trying to steal steven's money
The police have been notified.
>>> eric_account.balance
0
>>> check.deposited
False
>>> steven_account.balance
0
>>> steven_account.deposit_check(check)
42
>>> check.deposited
True
>>> steven_account.deposit_check(check) # can't cash check twice
The police have been notified.
"""
withdraw_fee = 1
interest = 0.01
def withdraw(self, amount):
return Account.withdraw(self, amount + self.withdraw_fee)
def deposit_check(account_holder, Check):
if account_holder == Check.payable_to and Check.amount > 0:
self.deposit(Check.amount)
Check.amount = 0
Check.deposited = True
return self.balance
else:
return 'The police have been notified.'
class Check(object):
"*** YOUR CODE HERE ***"
def __init__(self, payable_to, amount):
self.payable_to = payable_to
self.amount = amount
self.deposited = False
# Q8
class Keyboard:
"""A Keyboard takes in an arbitrary amount of buttons, and has a
dictionary of positions as keys, and values as Buttons.
>>> b1 = Button(0, "H")
>>> b2 = Button(1, "I")
>>> k = Keyboard(b1, b2)
>>> k.buttons[0].key
'H'
>>> k.press(1)
'I'
>>> k.typing([0, 1])
'HI'
>>> k.typing([1, 0])
'IH'
>>> b1.pressed
2
>>> b2.pressed
3
"""
def __init__(self, *args):
"*** YOUR CODE HERE ***"
self.buttons = {buts.pos: buts for buts in args}
def press(self, info):
"""Takes in a position of the button pressed, and
returns that button's output"""
"*** YOUR CODE HERE ***"
if info in self.buttons.keys():
self.buttons[info].pressed += 1
return self.buttons[info].key
return None
def typing(self, typing_input):
"""Takes in a list of positions of buttons pressed, and
returns the total output"""
"*** YOUR CODE HERE ***"
result = ''
for pos in typing_input:
result += self.press(pos)
return result
class Button:
def __init__(self, pos, key):
self.pos = pos
self.key = key
self.pressed = 0
|
|