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...
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
using repl or python class House():    valuationRate = 10       def __init__(self,city,sqft,price):        ...
using repl or python class House():    valuationRate = 10       def __init__(self,city,sqft,price):         self.city = city         self.sqft = sqft         self.price = price           def getPrice(self):         return self.price    def applyValuation(self):         self.price += self.price * self.valuationRate/100 # create class Townhouse that inherits from class House # class Townhouse should have valuationRate = 5    # implement method setPrice(self,price) in class Townhouse    # create an object House: city=Atlanta, sqft=10000, price=200000   ...
python class Node(): def __init__(self, value): pass class PostScript(): def __init__(self): pass    def push(self, value):...
python class Node(): def __init__(self, value): pass class PostScript(): def __init__(self): pass    def push(self, value): pass    def pop(self): return None    def peek(self): pass    def is_empty(self): pass def stack(self): pass    def exch(self): pass    def index(self): pass    def clear(self): pass    def dup(self): pass    def equal(self): pass    def depth(self): pass    stack = PostScript() def decode(command_list): for object in command_list: if object == '=': stack.equal() elif object == 'count': stack.depth() elif object ==...
*****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....
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...
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 %...
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 #:        ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT