Week 10: Classes I

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 10: Closure#

Syllabus#

  • Object-oriented programming, structuring code to bundle properties and behavior.

  • Classes, templates for creating instances that share common attributes and methods.

  • Instances, concrete objects created from a class template.

  • The keyword class

  • Class naming convention

  • Attributes

  • Objects are mutable

  • Methods and the argument self

  • The method __init__()

Checkpoints#

Checkpoint 10.1: Bank Account #

We want to create a class to represent a bank account, allowing for depositing and withdrawing money while ensuring the balance never goes negative.

Write the class definition for the class BankAccount . The balance must be stored in an attribute called balance . The __init__ method should take the initial balance as input. The deposit method should take as input an amount to deposit and add it to the balance. The withdraw method should take as input an amount to withdraw, subtract it from the balance, and return the amount withdrawn. If the withdrawal would result in a negative balance, the method should leave the balance unchanged and return 0. The get_balance method should return the current account balance.

Consider the example below.

>>> my_account = BankAccount(1000)
>>> my_account.get_balance()
1000
>>> my_account.deposit(500)
>>> my_account.get_balance()
1500
>>> my_account.withdraw(200)
200
>>> my_account.get_balance()
1300
>>> my_account.withdraw(2000)
0
>>> my_account.get_balance()
1300

In this example, the balance is 1000 initially. Then, 500 is deposited. Next, 200 is withdrawn, which is allowed since the balance before withdrawing is 1500. Finally, an attempt to withdraw 2000 is made, but since the current balance is only 1300, the withdrawal is not possible. Therefore the method returns 0, and the balance remains unchanged.

The filename and requirements are in the box below:

bank_account.py

BankAccount()

A class that represents a bank account.

__init__(balance)

Initialize the bank account with a balance.

Parameters:

  • balance

int, non-negative

The initial balance of the bank account.

deposit(amount)

Deposit money into the account.

Parameters:

  • amount

int, positive

The amount of money to deposit.

withdraw(amount)

Withdraw money from the account.

Parameters:

  • amount

int, positive

The amount of money to withdraw.

Returns:

  • int

The amount of money withdrawn, or 0 if the balance is insufficient.

get_balance()

Return the current balance.

Returns:

  • int

The current balance.

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

Checkpoint 10.2: Event Manager #

We want to create a class to represent an event (like a lecture or a concert), allowing for registering and de-registering participants, while preventing duplicate registrations.

Write the class definition for the class EventManager . The register method should take a name as input and add it to the list of registrations. If the name is already in the list, it should not be added again. The method should return True if the name was added, and False if it was not. The deregister method should take a name as input, remove it from the list of registrations and return True. If the name is not in the list it cannot be removed, and False should be returned. The get_num_registrations method should return the number of participants currently registered.

Below is an example of using the class.

>>> my_event = EventManager()
>>> my_event.get_num_registrations()
0
>>> my_event.deregister('Mike')
False
>>> my_event.register('Mike')
True
>>> my_event.register('Mike')
False
>>> my_event.register('John')
True
>>> my_event.deregister('Mike')
True
>>> my_event.get_num_registrations()
1

In this example, there ar no registrations initially. Then, an attempt to deregister Mike is made, but this is not possible. Then, Mike is registered. Then, an attempt to register Mike again is made, but this is not possible. Then, John is registered. Finally, Mike is deregistered. Finally, the number of registrations is checked and printed.

The filename and requirements are in the box below:

event_manager.py

EventManager()

A class that represents an event.

__init__()

Initialize the event with no registrations.

register(user)

Register the user as an event participant.

Parameters:

  • user

str

The name of the user to register.

Returns:

  • bool

True if the user was successfully registered, False otherwise.

deregister(user)

Deregister the user.

Parameters:

  • user

str

The name of the user to deregister.

Returns:

  • bool

True if the user was successfully deregistered, False otherwise.

get_num_registrations()

Return the number of users currently registered.

Returns:

  • int

The number of registered users.

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