Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Exercise10

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

CMSC 150 Fall 2024

Exercise 10 (15 points available)

1. Add a copy method to Book Class. Do no use the Python Copy library.
.
(5 points)
class Book:

def __init__(self, title, author, copies):


self.title = title
self.author = author
self. copies = copies

def change_number_of_copies(self,update_amount):
self.copies += update_amount

def calculate_fine(self, days_late):


return days_late * .1

def __str__(self):
return self.title + " by " + self.author + \
" with " + str(self.copies) + " copies."

class Book:
def __init__(self, title, author, copies):
self.title = title
self.author = author
self.copies = copies

def change_number_of_copies(self, update_amount):


self.copies += update_amount

def calculate_fine(self, days_late):


return days_late * 0.1

def __str__(self):
return self.title + " by " + self.author + " with " + str(self.copies) + " copies."

# Copy method
def copy(self):
return Book(self.title, self.author, self.copies)

# Example usage
book1 = Book("1984", "George Orwell", 5)
book2 = book1.copy()
print(book1) # Output: 1984 by George Orwell with 5 copies.
print(book2) # Output: 1984 by George Orwell with 5 copies.

2. Using the Car class shown below, add a class attribute (make sure it is private) to keep track
of the number of car objects created and a static method that will return this value. Create
three objects of this type. Use the static method to show that three car objects have been
created.
(5 points)

class Car:

#Constructor
def __init__(self, make, model, mpg):
self.make = make
self.model = model
self.mpg = mpg

def __str__(self):
return self.make + " " + self.model + \
" gets " + str(self.mpg) + " miles to the gallon"

class Car:
# Private class attribute
_car_count = 0

# Constructor
def __init__(self, make, model, mpg):
self.make = make
self.model = model
self.mpg = mpg
Car._car_count += 1 # Increment car count when a new object is created

def __str__(self):
return self.make + " " + self.model + " gets " + str(self.mpg) + " miles to the gallon."

# Static method to get the count of cars created


@staticmethod
def get_car_count():
return Car._car_count
# Example usage
car1 = Car("Toyota", "Corolla", 30)
car2 = Car("Honda", "Civic", 35)
car3 = Car("Ford", "Focus", 28)

print(Car.get_car_count()) # Output: 3

3. Create a Textbook class that is a subclass (child class) of Book. It should have one additional
instance variable - subject. There should be the following methods:
(5 points)

a. The constructor which calls the superclass (parent classes)’s constructor to set the Parent
instance variables and then sets the subject
b. A method to calculate a fine (which takes the number of days as a parameter, multiplies it by
10 and then adds 1.00 to the fine)
c. __str__ method which calls the parent __str__ method and then adds on the subject
Test all methods

class Textbook(Book):
def __init__(self, title, author, copies, subject):
# Call the parent class constructor
super().__init__(title, author, copies)
self.subject = subject

# Override the calculate_fine method


def calculate_fine(self, days_late):
return days_late * 10 + 1.00

# Override the __str__ method


def __str__(self):
return super().__str__() + f" (Subject: {self.subject})"

# Example usage
textbook = Textbook("Physics for Scientists and Engineers", "Raymond Serway", 3,
"Physics")
print(textbook) # Output: Physics for Scientists and Engineers by Raymond Serway with 3
copies. (Subject: Physics)
print(f"Fine for 3 days late: ${textbook.calculate_fine(3):.2f}") # Output: Fine for 3 days
late: $31.00

You might also like