Lab12 Handout v2
Lab12 Handout v2
Goal
• Learning about classes and objects, enabling the creation of custom data
types.
• Developing crucial debugging skills to identify and fix various types of
errors in code.
Tasks
1. Guide you through the process of Creation of custom data types using
classes.
2. Developing essential debugging skills by identifying and fixing errors.
3. Applying object-oriented programming concepts to real-world scenarios.
Deadline:
It is an academic offense to copy code from other students, provide code to other students, let other
people (including the teaching staff) debug your code, or debug other students’ code.
You are highly recommended to attempt the questions yourself before you look at the solution as a
way to learn.
Your first task is to design and create the Rectangle class. Follow these steps:
In Python, a class is defined using the class keyword, followed by the class name (in this
case, "Rectangle"). Make sure the class name follows the PEP-8 convention, using lowercase
letters and underscores to separate words.
The init method is a constructor that initializes the class's instance variables. It is called
when an object of the class is created. It accepts the parameters length and width.
• Within the init method, initialize the instance variables length and width by assigning
the values passed as arguments to them and make sure the parameters are valid.
Instance variables are attributes that store data for each object created from the class. In
this case, we need to store the length and width of the rectangle.
Solution:
Methods in a class are functions that can perform operations on the class's data (instance
variables).
• Implement the calculate_area method by writing code that calculates the area of the
rectangle. The area of a rectangle is calculated by multiplying its length and width.
• The calculate_area method should return the computed area as its result.
Solution:
• Implement the calculate_perimeter method by writing code that calculates the perimeter of
the rectangle. The perimeter of a rectangle is calculated by adding the lengths of all four
sides, which is 2 times the sum of length and width.
• The calculate_perimeter method should return the computed perimeter as its result.
Solution:
Solution:
Note: Here we provide example code for getter and setter for the variable length. Please
write your own getter and setter for the variable width.
Think about what the advantage is to use setter and getter methods compared with manipulating the
attributes directly.
Rubrics:
• You will get full mark for this question if you submit the solution (with no typo) to the required
location on Prairielearn before the deadline.
Note:
Requirement
• Your submission must be based on the provided code. There are simpler ways of doing this, but
you cannot delete all the provided code and write your own.
Submission
Copy the Python code to Lab 12 -> Task 2 on Prairielearn.
You can resubmit your code as many times as you need, but you need to wait for 5 minutes before
submission (You need spend some time debugging!).
Rubrics
• You will not receive any mark if you do not submit it to the designated location on Prairielearn
before the deadline.
• You will not receive any mark if your code violates the above requirements.
• You will receive 5 points if your code passes the test for the constructor for class Product
• You will receive 5 points if your code passes the test for the reduce_quantity method for class
Product. In this test case:
>>> laptop = Product("laptop", 2030.5, 2)
>>> laptop.reduce_quantity(3)
>>> laptop.get_quantity()
0
• You will receive 5 points if your code passes the test for the _find_product method for class
Inventory. In this test case:
>>> laptop = Product("laptop", 2030.5, 2)
>>> retail_store_inventory = Inventory()
>>> retail_store_inventory._products = [laptop]
>>> retail_store_inventory._find_product("laptop") is laptop
True