Programming Classes In this part of the lab we will create a program by writing
ID: 3689116 • Letter: P
Question
Programming Classes In this part of the lab we will create a program by writing two classes. One that represents books and one that represens a library, or a collection of books. This should help in the second part of the lab where you create classes to make music. Crete a Python file with name Library py. In this file you should create 2 classes: Book and Library. The Book class should have the following methods:_init_(self, author, title, data=0) This method just saves its arguments author, title, and date in instance variables. Note the default value for the date. This means that both Book ("Herrman Melville", "Moby Dick", 1851) and Book("Herman Melville", "Moby Dick") are valid construction; the second one uses the default value 0 for the date. Create a Library class that has a class variable that will store information about all of the books in the library. addBook(self, b) Here b is a Book object. Rhis adds b to the list created in_int_ and increments the Library bookCount class variable. Sort(self) This sorts the list created in_ini_ according to the order represened by the_it_ method of the Book class Print(sell) This has a loop that runs through the library's list of books and prints each of them Now addd a main() method to your program that creates and empty library and the creates each of following books and adds them to the library. After all books are added dort the library then print it. Bocks: On the Origin of Species" by Darwin (1859), "The Canterbury Tales" by Chaucer. "The Voyage of the Beale" by Darwin (1839). Catch-22" by Heller (1981). "Tulips & Chimneys" by Cummings, "Regeneration" by Banker (1991).Explanation / Answer
Part 1
import os
class Book(self):
def __init__(self,author,title,date =0):
self.author = author;
self.title = title;
self.date = date;
def setDate(self,d):
self.date = d;
def __str__(self):
return '%s%s' self.author,self.title
def __lt__(self,other):
self.other = self.other;
class Library(Book)
Book bookCount =0;
lib_book=[];
def __init__(self):
self.lib = bookCount;
def addBook(self,b):
self.lib = b;
Library.bookCount++;
lib_book.append(self.lib)
def Sort(self):
return self.lib;
def Print(self):
for eachbook in lib_book:
print eachbook
libObj = Library('rampal','Amazon book');
libObj.setDate('15/11/1876')
libObj.addBook('appleBook')
libObj.Sort()
libObj.Print()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.