SmartFridge and Keyboard Architecture at Computer Science 61A
Computer Science 61A students at the University of California, Berkeley explored object-oriented programming paradigms during discussion sections, examining custom class definitions through practical implementations like a smart refrigerator tracker and a mechanical keyboard simulator, according to course materials published at cs61a.org. The curriculum focuses on determining instance attributes, class attributes, and method implementations using dictionaries and iterators. The discussion worksheet guides students through building a `SmartFridge` class that uses an internal Python dictionary to track inventory names as keys and quantities as values. When items run out or fall below zero during a `use_item` call, the class triggers automated prompts reminding owners to restock.
Object-Oriented Design Principles in Python Programming
The `SmartFridge` implementation requires students to write two primary methods: `add_item` and `use_item`. The `add_item` method accepts an item string and a float quantity, updates the internal dictionary, and returns a formatted string confirming the current stock level using Python f-strings. By leveraging dictionary comprehensions within the `Keyboard` initialization method, the program maps lowercase letters of the alphabet to individual button objects configured to append typed characters directly to a running list.
Inheritance Hierarchies for Bear Drawing Implementations
Beyond basic class construction, the curriculum challenges students to apply inheritance principles via the `SleepyBear` and `WinkingBear` subclasses. By inheriting from a base `Bear` class and overriding the `next_eye` method, students modify facial expressions without duplicating existing rendering logic from the `Eye` class. Exam practice materials from the Fall 2024 final exam further test these concepts with the `Counter` and `MissDict` classes.
Frequently Asked Questions
What is the primary purpose of the SmartFridge class in the curriculum?
The `SmartFridge` class teaches students how to manage state inside an object by using dictionaries to track item quantities, update inventory dynamically, and return conditional string notifications when supplies run low.
How does the Keyboard class handle uppercase and lowercase letter generation?
A `Keyboard` relies on a shared `CapsLock` instance that tracks how many times it has been pressed, allowing individual `Button` objects to evaluate whether their letter attribute should be transformed using `.upper()` before passing it to an output function.
How do subclasses customize behavior in the Bear inheritance exercises?
Subclasses like `SleepyBear` and `WinkingBear` override specific methods such as `next_eye()` to alter inherited attributes—like closing an eye or mixing closed and open states—while retaining the core drawing logic of the parent `Bear` class.
Worth a look