CS51A - Spring 2019 - Class 24

Example code in this lecture

   queue.py
   stack.py
   fruit.py
   rectangle3.py
   rectangle2.py

Lecture notes

  • administrative
       - no lab/class Friday
       - assignment 8 posted later today, due next Thursday
       - midterm next Monday. Sample problems posted soon.
       - extra mentor hours
          - Saturday 1-3pm (Janice)
          - Sunday 7-9pm (Simon)
          - Monday 6-8pm (Cleo)

  • classes: a "class" is the blueprint describing what data and methods an object will have

  • look at the Queue class in queue.py code
       - has 5 methods (constructor, str, and three methods we wrote
       - what data does it keep, i.e. what are the instance variables?
          - just self.queue, which is a list
       - constructor
          - the constructor has an optional parameter and can be called with either zero parameters or with a list:
             
             >>> q = Queue()
             >>> print(q)
             The queue contains: []
             >>> q = Queue([1, 2, 3, 4])
             >>> print(q)
             The queue contains: [1, 2, 3, 4]

          - if it's given a list as a parameter it *copies* it using slicing (:) and saves that away in the instance variable
             - why copy it?
                - to avoid aliasing!
                - otherwise, the instance variables (self.queue) would reference the same list as was passed in (a bad thing!)

       - look at add and remove. What is this class?
          - way of storing data
             - implemented like a line
             - first things to be added are the first things to be removed
             - sometimes called FIFO (first in first out)
          - add adds elements to the *back* of the list
          - remove removes elements from the *front* of the list

       - Example usage:
          >>> q = Queue()
          >>> q.add(1)
          >>> q.add(10)
          >>> q.add(2)
          >>> print(q)
          The queue contains: [1, 10, 2]
          >>> q.remove()
          1
          >>> q.remove()
          10
          >>> q.remove()
          2
          
       - is_empty
          - just checks if the queue has anything in it

       - notice that underneath the covers, this is still just a list
       - by hiding the list in the class, we have:
          - provided a clear small set of methods that defines how we can interact with the object (the queue)
          - hid the implementation details from whoever uses it
             - we used a list, but could have used something else
             - in a similar way, we could have added to the front of the list and removed from the back and still achieved exactly the same functionality

  • look at Stack class in stack.py code
       - what does it do?
          - same basic interface as the Queue class
             - constructor and str methods
             - is_empty
             - a method to add things (push)
             - a method to remove things (pop)
       
       - the difference is that stacks add and remove things from the *same* end of the list
          - think of it like a stack of plates
             - the last plate that you put on top
             - will be the first one to be removed
             - sometimes called LIFO (last in first out)

       - For example, if we add the same elements as we did before, we get them in the opposite order
          >>> s = Stack()
          >>> s.add(1)
          >>> s.add(10)
          >>> s.add(2)
          >>> s.remove()
          2
          >>> s.remove()
          10
          >>> s.remove()
          1

  • We're going to design a Fruit class. It will have the following constructor and methods:
       def __init__(self, name, color):
          self.name = name
          self.color = color
          self.eaten = False
          self.age = 0

       - is_eaten takes zero arguments and returns a boolean indicating whether or not the fruit is eaten
       - eat takes zero arguments and "eats" the fruit
       - allergy_check takes a color and returns true if the fruits color is the same as the input color, false otherwise
       - age_fruit takes zero arguments and ages the fruit by a day
       - __str__ prints out a string version of the fruit

       - For example, if we run:
       
          fruit = Fruit("banana", "yellow")
          print(fruit)
          print(fruit.allergy_check("red"))
          fruit.age_fruit()
          print(fruit)
          print(fruit.is_eaten())
          fruit.eat()
          print(fruit.is_eaten())

       we would get:

          yellow banana that is 0 days old
          False
          yellow banana that is 1 days old
          False
          True

       - Look at Fruit class in fruit.py code

  • Look at the Rectangle class in rectangle3.py code
       - Another version of the Rectancle class that we saw last week
       - Like the code from rectangle2.py code, we keep track of the x,y coordinates of the lower left-hand corner and the width and height
       
       - If we print out the rectangle we see the position of the rectangle and the area:
          >>> r = Rectangle(0, 0, 10, 20)
          >>> print(r)
          >>> Rectangle at (0, 0) with area: 200

       - In the __str__ method, we call the area method
          - anytime you want to call another method from within the class you write self.method_name, e.g. self.area()

       - The equals method takes one parameter as input. What is the parameter?
          - Another rectangle!
          - equals is method we wrote that takes one parameter, another rectangle, as input
          - in the body of the method then there are two rectangles:
             - self
             - another_rectangle

       - We can access the instance variables of the parameter rectangle (another_rectangle) in the same way we can self.