Week 11: Classes II

Warning

This page shows last year’s version. We might still make small changes, but you’re welcome to take a look. We’ll remove this notice once the page is final.

Week 11: Closure#

Syllabus#

  • Inheritance: parent class (superclass) and child class (subclass)

  • Method overriding

  • The super() function

  • Method __str__() and overloading str() and print()

  • Operator overloading example on __add__() and __eq__()

Checkpoints#

The checkpoints this week are extensions of the checkpoints from last week. You should put your solutions in the same files as last week.

Checkpoint 11.1: Overdraft Account #

We want to create a subclass of the BankAccount class from Checkpoint 10.1: Bank Account . This subclass should allow the user to have a negative balance, as long as the sum of the balance and the overdraft limit is not negative.

Write the class definition for the subclass OverdraftAccount , which inherits from BankAccount . Each instance of the subclass should store the overdraft limit. The constructor of the new class should take as input the initial balance and the overdraft limit (a non-negative integer). The withdraw method should ensure that the overdraft limit is not exceeded. If the withdrawal is not possible, the balance should remain unchanged. As before, the withdraw method should return the amount withdrawn. You should modify the necessary methods of the class to achieve this behavior, and inherit the rest of the methods from the parent class.

You should write the class definition for OverdraftAccount in the same file as the class definition for BankAccount .

Refer to the example below for expected behavior.

>>> my_account = OverdraftAccount(0, 500)
>>> my_account.get_balance()
0
>>> my_account.deposit(1000)
>>> my_account.get_balance()
1000
>>> my_account.withdraw(1300)
1300
>>> my_account.get_balance()
-300
>>> my_account.withdraw(500)
0
>>> my_account.get_balance()
-300

In this example, the initial balance is 0, and the overdraft limit is 500. First, 1000 is deposited. Then, 1300 is withdrawn. This is allowed since it brings the balance to -300 which is above -500. Finally, 500 is attempted to be withdrawn, but that would bring the balance below -500. So, this withdrawal is not possible, and the balance remains unchanged.

The filename and requirements are in the box below:

bank_account.py

OverdraftAccount()

A class that represents a bank account allowing overdraft.

__init__(balance, overdraft_limit)

Initialize the overdraft account with a balance and an overdraft limit.

Parameters:

  • balance

int, non-negative

The initial balance of the bank account.

  • overdraft_limit

int, non-negative

The overdraft limit of the bank account.

withdraw(amount)

Withdraw money from the bank account.

Parameters:

  • amount

int, positive

The amount of money to withdraw.

Returns:

  • int

The amount of money withdrawn, or 0 if the withdrawal fails.

Use the following script to check your function test_overdraft_account.py. If your function fails the test in this script, it will also fail when you hand it in.

Checkpoint 11.2: Limited Event Manager #

We want to create a subclass of the EventManager class from Checkpoint 10.2: Event Manager . This subclass should prevent the registration of participants if the limit on the number of participants has been reached.

Write the class definition for the subclass LimitedEventManager , which inherits from EventManager . Each instance of the subclass should store the registration limit. The constructor of the new class should take as input the registration limit (a non-negative integer). The register method should ensure that the number of registrations does not exceed the limit. If the registration is not possible because the limit is reached, the method should return False . If the limit is not reached, the registration method should act as specified in EventManager . You should modify the necessary methods of the class to achieve this behavior, and inherit the rest of the methods from the parent class.

An example of using the class is shown below.

>>> my_event = LimitedEventManager(3)
>>> my_event.register('Mike')
True
>>> my_event.register('Emily')
True
>>> my_event.register('Sara')
True
>>> my_event.register('Peter')
False
>>> my_event.get_num_registrations()
3

In this example, there are no registrations initially. Then, Mike is registered, and then Emily and Sara are registered. Then, an attempt to register Peter is made, but this is not possible as 3 people are already signed up. Finally, we get and print the number of registrations.

The filename and requirements are in the box below:

event_manager.py

LimitedEventManager()

A class that represents an event with limited number registrations.

__init__(registration_limit)

Initializes the limited event with a registration limit.

Parameters:

  • registration_limit

int, non-negative

The maximum number of allowed registrations.

register(user)

Register the user if limit is not reached.

Parameters:

  • user

str

The user name to register.

Returns:

  • bool

True if the user was successfully registered, False otherwise.

Use the following script to check your function test_limited_event_manager.py. If your function fails the test in this script, it will also fail when you hand it in.