Question

In: Computer Science

By using Python: a. Make a class called Book. The __init__() method for Book should store...

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.

Solutions

Expert Solution

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------------------

Related Solutions

PYTHON ASSIGNMENT Problem: (1) The  __init__ method should initialize the values of the instance variables. Here is...
PYTHON ASSIGNMENT Problem: (1) The  __init__ method should initialize the values of the instance variables. Here is the beginning of __init__: def __init__(self, the_hr, the_min, the_sec): self.hr = the_hr # Also initialize min and sec. (2) Include a __str__ method that returns the current state of the clock object as a string. You can use the string format method format like this: return "{0:02d}:{1:02d}:{2:02d}".format(self.hr, self.min, self.sec) (3) When the tick method is executed, add one to sec. If the resulting value...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This method accepts from a user (1) an integer hourly rate and (2) an integer total number of hours worked in a week. It calculates and displays the total weekly wage of an employee. The company pays straight time for the first 40 hours worked by an employee and times and a half for all the hours worked in excess of 40. This class should...
Language: Python Implement the Book class as demonstrated below. You should not change the Book methods...
Language: Python Implement the Book class as demonstrated below. You should not change the Book methods and implementation as provided. The docstrings in the template contain descriptions of each method. >>> b=Book ( ' Code' , 'Charles Ptzold' 'Computing' , 2001) >>> b Book ( ' Code' , 'Charles Ptzold' 'Computing' , 2001) >>> str (b) ' Code : Charles Ptzold: Computing: 2001 ' >>> b. getTitle ( ) ' Code ' >>> b. getAuthor() ' Charles Ptzold' >>> b....
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Using Python 3, Write a class called GolfScore that keeps track of the score for a...
Using Python 3, Write a class called GolfScore that keeps track of the score for a person playing a round of golf. We will limit the round to 9 holes, just to make testing easier. As a reminder, the holes on the golf course are numbered 1 – 9. Create a class level variable named NUM_OF_HOLES set to 9. Methods to be written: The constructor – sets the score for all holes to -1 addScore (hole, score) – this method...
For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)        &nb
For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)               style = color                return    def plane(self,oil)              liquid = self.oil + self.truck(color) For the plane method, I am getting an error that the class does not define __add__ inside init so I cannot use the + operator. How do I go about this.             
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT