CS51A - Spring 2025 - Class 17
Example code in this lecture
queue.py
stack.py
fruit.py
rectangle3.py
rectangle2.py
Lecture notes
administrative
- assignment 7
- assignment 8 this week
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 other methods
- 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 adds each item in the list to the queue
- If you want to call a method from another method in the class you need to use self.method_name
- recall, "self" is a reference to the current object
- 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 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 (sometimes called push)
- a method to remove things (sometimes called 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 Rectangle 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 a 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.
Look at the Rectangle class in rectangle3doc.py
- Classes also have docstrings that give a brief overview of the class and it's role
- If we ask for help on the Rectangle class, it will give us the class docstring as well as the docstrings for all of the methods
>>> help(Rectangle)
class Rectangle(builtins.object)
| Rectangle(x1, y1, x2, y2)
|
| Rectangle represented with location and size
|
| Methods defined here:
|
| __init__(self, x1, y1, x2, y2)
| Construct a rectangle based on two corners:
| (x1, y1) and (x2, y2).
|
| :param int x1:
| :param int y1:
| :param int x2:
| :param int y2:
|
| __str__(self)
| Rectangle location and area.
|
| :return: (str) representing the rectangle
|
| area(self)
| Calculate the area of the rectangle.
|
| :return: the area
|
| equals(self, another_rectangle)
| Determine if another_rectangle is equivalent
| to the current rectangle. A rectangle is equivalent
| if it is at the same x,y location and has the same
| dimensions.
|
| :param Rectangle another_rectangle: another rectangle
| :return: (bool) True if the other rectangle is equal