A class called Book is designed to model a book written by one author. It contains: • Four private instance variables: name (String), author (of the class Author you have just created, assume that a book has one and only one author), price (double), and qty (int); • An __init__() to initialize the name, author, price, and qty with the given values; • Getters/Setters: getName(), getAuthor(), getPrice(), setPrice(), getQty(), setQty(). • __str__() method that returns printable string of that object with the following format: "Book[name=?,Author[name=?,email=?,gender=?],price=?,qty=?". You should reuse Author’s __str__(). Write the Book class (which uses the Author class written earlier). Also write a test driver codeto test all the methods in the class Book. Take Note that you have to construct an instance of Author before you can construct an instance of Book. E.g., ahTeck = Author("Tan Ah Teck", "ahteck@nowhere.com", 'm') # Test the constructor print(ahTeck) # Test __str__() dummyBook = Book("Java for dummy", ahTeck, 19.95, 99) # Test Book's Constructor print(dummyBook); # Test __str__() # Test Getters and Setters dummyBook.setPrice(29.95) dummyBook.setQty(28) print ("name is: " , dummyBook.getName()) print ("price is: " , dummyBook.getPrice()) print ("qty is: " , dummyBook.getQty()) print ("Author is: " , dummyBook.getAuthor()) # Author's __str__() print ("Author's name is: " , dummyBook.getAuthor().getName()) print ("Author's email is: " , dummyBook.getAuthor().getEmail()) Take note that both Book and Author classes have a variable called name. However, it can be differentiated via the referencing instance. For a Book instance says aBook, aBook.name refers to the name of the book; whereas for an Author's instance say auAuthor, anAuthor.name refers to the name of the author. There is no need (and not recommended) to call the variables bookName and authorName. Save your code as book.py.
A class called Book is designed to model a book written by one author. It contains:
• Four private instance variables: name (String), author (of the class Author you have just
created, assume that a book has one and only one author), price (double), and qty (int);
• An __init__() to initialize the name, author, price, and qty with the given values;
• Getters/Setters: getName(), getAuthor(), getPrice(), setPrice(), getQty(), setQty().
• __str__() method that returns printable string of that object with the following format:
"Book[name=?,Author[name=?,email=?,gender=?],price=?,qty=?".
You should reuse Author’s __str__().
Write the Book class (which uses the Author class written earlier). Also write a test driver
codeto test all the methods in the class Book. Take Note that you have to construct an instance
of Author before you can construct an instance of Book. E.g.,
ahTeck = Author("Tan Ah Teck", "ahteck@nowhere.com", 'm') # Test the constructor
print(ahTeck) # Test __str__()
dummyBook = Book("Java for dummy", ahTeck, 19.95, 99) # Test Book's Constructor
print(dummyBook); # Test __str__()
# Test Getters and Setters
dummyBook.setPrice(29.95)
dummyBook.setQty(28)
print ("name is: " , dummyBook.getName())
print ("price is: " , dummyBook.getPrice())
print ("qty is: " , dummyBook.getQty())
print ("Author is: " , dummyBook.getAuthor()) # Author's __str__()
print ("Author's name is: " , dummyBook.getAuthor().getName())
print ("Author's email is: " , dummyBook.getAuthor().getEmail())
Take note that both Book and Author classes have a variable called name. However, it can be
differentiated via the referencing instance. For a Book instance says aBook, aBook.name refers
to the name of the book; whereas for an Author's instance say auAuthor, anAuthor.name refers
to the name of the author. There is no need (and not recommended) to call the variables
bookName and authorName.
Save your code as book.py.
Step by step
Solved in 4 steps with 2 images