Exercises For May 2023
Exercises For May 2023
1. Library
Create a class called Book whose constructor's parameters are title, author, number of pages, and type
of a book (novel, fairy tale, detective, etc.). The class also has a method print_info, which prints the
book's information on the screen in a readable form.
Create a class called Library, whose constructor creates an empty list of books. The class must have
the following methods:
• Method add_book, which takes an instance of the class Book as an argument and adds it to
the list of books.
• Method print_books, which prints the information of all the books in the library using the
method print_info.
• Method borrow_book, whose argument is the book title as a string. The capitalization of
letters in the argument may not be correct. The method searches for a book with that title in
the list of books. If the book exists in the list, then removes it from the list and returns the
book instance found. If the book doesn't exist, then leaves the list unchanged and returns the
value None.
Write the main program that creates an instance of the Library class, adds all the books in the
file books.txt to it, and prints all the books in the library to the screen. It then asks the user for the title
of the book they want to borrow. If a book with this name is in the library, the program borrows it and
again prints all books in the library. If the book is not in the library, the program asks the user for the
name of the book again until the user enters the title of an existing book. After that the program
terminates.
2. Scooter rental
Create a class called Scooter, whose constructor's parameters are the name of the rental company,
the starting fee for a ride, the price per 100 meters of riding, and the available riding distance of the
scooter in kilometers.
• Method price, which takes the length of the ride in kilometers as an argument. If the ride
length requested does not exceed the available riding distance of the scooter, the method
returns the price of a ride of that length, taking into account the starting fee and the price per
100 meters. However, if the ride length exceeds the scooter's available riding distance, the
method returns the integer 1000.
• Method ride, which takes the length of the ride as an argument and subtracts it from the
available riding distance. If the available riding distance becomes exhausted, it assigns the
value 0 to the distance field.
• Method charge, whose argument is the number of kilometers, and which increases the
available riding distance of the scooter by the given number of kilometers.
Create a class called Rental whose constructor's parameter is a list of instances of the class Scooter.
• Method display_choices, which takes the length of the ride in kilometers as an argument and
prints the name of each scooter and the price of the ride, ordering the scooters from top to
bottom, starting with the cheapest. To find the price of the ride, use the price method.
• Method rent, whose arguments are the name of the scooter the user wants to ride and the
length of the ride. If the requested length does not exceed the available riding distance of the
scooter, then it prints the price of the ride and rides the scooter so many kilometers
(method ride). Otherwise, it prints an appropriate message and does nothing.
• Method charge_scooter, which takes the name of the scooter to be charged and the number
of kilometers as arguments. The method increases the available riding distance of the scooter
by the requested number of kilometers (method charge).
Finally, write a program that prompts the user for the details of three scooters:
The data for each scooter is entered as one line. All three scooters are added to the list of scooters of
an instance of the Rental class, and then the following methods are called on that instance:
• display_choices(2)
• rent("Bolt", 3)
• rent("Tuul", 18)
• rent("Tuul", 5)
• charge_scooter("Tuul", 5)
• rent("Tuul", 2)