In: Computer Science
By using Python:
a. Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library.
b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods.
c. Create three different instances from the class, and call book_info() for each instance.
d. Add an attribute called number_reads with a default value of 0. Create a new instance called book5 from this class. Print the number of times the book has been read, and then change this value and print it again.
e. Add a method called set_number_reads() that lets you set the number of times the book has been read. Call this method with a new number and print the value (number_reads) again. f. Add a method called increment_number_reads() that lets you increment the number of customers who've been served. Call this method with any number you like.
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code. Below is the output of the program: Below is the code to copy: #CODE STARTS HERE----------------
#Solution a: class Book: #Class book #Init function. Set number_reads to 0 by default def __init__(self,title,author,number_reads=0): self.title = title self.author = author self.number_reads = number_reads #Prints attribute values def book_info(self): print("\nTitle:",self.title) print("Author:",self.author) print("Number reads:",self.number_reads) #Prints that the book is available def book_available(self): print(self.title,"book is available in the library") #Sets the number_reads attribute to a given number def set_number_reads(self, num): self.number_reads = num #Increments number_reads by 1 def increment_number_reads(self): self.number_reads+=1 #Solution b: print("Solution b:") book1 = Book("Demo book1","Demo Auth1") #Create an object print("Printing individually:\n",book1.title,"\n", book1.author) #Print separrately print("Printing from class:",end="") book1.book_info() #Print by calling class methood #Solution c: print("\nSolution c:") #Create and print 3 class book2 = Book("Demo book2","Demo Auth2") book2.book_info() book3 = Book("Demo book3","Demo Auth3") book3.book_info() book4 = Book("Demo book4","Demo Auth4") book4.book_info() #Solution d: print("\nSolution d:") #Create a class book5 book5 = Book("Demo book5","Demo Auth5",5) book5.book_info() #Print objectcbook5 book5.number_reads = 10 #Change number_reads manually book5.book_info() #Solution e: print("\nSolution e:") book6 = Book("Demo book6","Demo Auth6",6) book6.book_info() #Print book 6 book6.set_number_reads(12) #set number_reads by calling method book6.book_info() #Print updated number reads #Solution f: print("\nSolution f:") book6.increment_number_reads() #Increment number_reads by 1 and print object book6.book_info() #CODE ENDS HERE------------------