A201 Introduction to Programming 1

A201 Lab 13

Date: Tuesday, November 28, 2023.
Due Date: Tuesday, December 5, 2023.

Goal: to define a class in Python.

Create a Python file called lab13.py and add your name and the lab number as a comment at the top.

Ex. 1. The Car class.

a. Class and objects

Download the Car.py file discussed in class.

In the main, declare two more variables and assign them objects of type Car with the values you want for the parameters. In the main, call the function show from these two variables. Then call the function olderThan from the first one using the second one as a parameter and print out the result.

b. Method sameCar.

Add a class method in the class Car called sameCar(self, other) that compares all 4 attributes of the two objects (make, model, color, year) and returns true if all 4 are equal, and false if not.

In the main, make a call of this method from one of the objects you created with the second one as a parameter and print the result.

Place the call to main in a test
if __name__ == '__main__':

c. Method price

Add a method to the class called price(self, initial) that takes one parameter representing the initial price of the car when it was new. The method should remove 7% of the price for each year that the car was used, up to a maximum of 70%, and return this value. For this, you can check if the car is more than 10 years old, and if it is, then return 0.3*initial price. If not, compute the price percentage left as 1 - 0.07 multiplied by the difference between 2020 and the year of the car. Then return the percentage times the initial price of the car.

In the main, ask the user to input a value for the initial price of a car, then call the function price from one of the cars you created, and output the result.

d. Importing the module

In the file lab13.py add a line importing the module Car. Move the function main from the Car module into lab13.py, together with the call for it. Test this file to verify that it works.

Lab Submission

Upload the files Car.py that you modified and lab13.py in Canvas, Assignments, Homework 13. You can wait until you finish the homework to upload all the files at the same time.