Lab 19: Classes

In this lab, you'll get practice creating and using objects for existing classes as well as add functionality to a class.

Rectangle class

Consider the following Rectangle class from the lecture along with the code that creates two different rectangles and uses them for a simple calculation.

class Rectangle:
    """
    Used for representing rectangles
    
    attributes: length, width
    """
    def __init__(self, starting_length, starting_width):
        self.length = starting_length
        self.width = starting_width
        
    def area(self):
        return self.length*self.width
    
    def perimeter(self):
        return 2*self.length + 2*self.width
    
def main():
    r1 = Rectangle(100,50) #r1 is a Rectangle object
    r2 = Rectangle(10,20) #r2 is a Rectangle objecct

    if r1.area() > r2.area():
        print("The larger rectangle has dimensions",r1.length,"by",r1.width)
    else:
        print("The larger rectangle has dimensions",r2.length,"by",r2.width)

main()
The larger rectangle has dimensions 100 by 50

Exercise 1: Change the code so that both the length and width of one of the rectanges is 25. Hint: you shouldn't need to change any code in the Rectangle class - just the code that instantiates Rectangle objects.

Exercise 2: Write some code that creates three new rectangles with different dimensions. Again, you shouldn't need to change the code in the Rectangle class, and you shouldn't need to define a new class - create three new Rectangle objects that are all from the same original class.

Exercise 3: Create a Cuboid class for representing three-dimensional objects with a length, width, and height. Examples of real-world cuboids are things like six-sided dice, bricks, etc. Come up with at least two methods that would be useful for a Cuboid to have.

Exercise 4: Create two Cuboid objects and practice calling their methods.

BankAccount class

Consider the following BankAccount class from the lecture.

class BankAccount:
    """
    A class for creating objects representing bank accounts
    
    attributes:
        balance - amount of money in the account
        customer_name - the customer's name
        interest_rate - interest rate for the account
    """
    def __init__(self, starting_customer_name):
        self.customer_name = starting_customer_name
        self.__balance = 0
        self.interest_rate = 0.0
        
    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount
        else:
            print("Error: the deposit amount must be positive.")
    
    def apply_interest(self):
        self._balance = self.__balance * (1+self.interest_rate)
        
    def display_info(self):
        print("Account Holder:",self.customer_name)
        print("Balance:",self.__balance)
        print("Interest Rate:",self.interest_rate)

Exercise 5: Create a program that does the following:

  • ask the user their name and an initial deposit amount
  • create a BankAccount object for them, and call the deposit() method for the initial deposit
  • display their account information with the display_info() method

Hint: You should not need to change the code inside the class at all - you're just creating objects of this class.

Exercise 6: Add a new withdraw() method to the BankAccount class that allows for money to be subtracted from the account's balance. Make sure to give an error if they try to withdraw more money than is in the account.

Challenge Exercise 7: Write a program that uses the BankAccount class in some way - maybe it's a menu that lets users interact with a bank account - presenting options for opening, depositing, or withdrawing from their account. Or, maybe you have another idea for how to use this class - anything is fine as long as it creates a BankAccount object and calls your withdraw() method at least once. You're also welcome to add additional attributes and methods that might be useful for your program (though you are not required to). When you are finished, submit your code to the Lab 19: BankAccount class assignment on codePost (there is no automated testing of this one).