CS 115 Lab 12, Part A: Practice: classes

[Back to lab instructions]


Instructions

Before you start writing your own code, you'll use the the online Python 3 tutor to practice using classes.

  1. Enter the following code from your pre-lab into the tutor:
    class Rectangle:
        def __init__(self, L, W):
            self.set_length(L)
            self.set_width(W)
    
        def set_width(self, W):
            self.width = W
    
        def set_length(self, L):
            self.length = L
    
        def get_width(self):
            return self.width
    
        def get_length(self):
            return self.length
    
        def get_area(self):
            return self.get_length() * self.get_width()
    
    # This code uses the Rectangle class
    r1 = Rectangle(1, 2) # Statement 1
    r2 = Rectangle(5, 6) # Statement 2
    a1 = r1.get_area() # Statement 3
    a2 = r2.get_area() # Statement 4
    
  2. Modify the definition of the Rectangle class to add a get_perimeter method. This method should return the perimeter of the rectangle. Then add a statement to the end of the program to test your new method.
  3. Demo your new method for an instructor. As part of the demo, you will be asked to step through the entire program and answer questions about how it works.
  4. Continue to Part B.