Question: a) In the Book class, add a display() method that displays a description of the book (author and title). The print() method is used in
a) In the Book class, add a display() method that displays a description of the book (author and title). The print() method is used in the main TestBook main program provided here. The statement System.out.println(book) where book designates one of the Book objects that you created. Launch the execution of the TestBook class.
b) Add a toString() method that returns a string that describes the book. Give the toString() method the same profile as the method of the same name in the java.lang.Object class (look in the JDK APIs). Run the TestBook class again. See what is now displayed by the System.out.println(book) instruction.
println() automatically uses the toString() method of the class of the object it has to print.
You have to know how to look in the API documentation (javadoc). Starting from the java.lang.System class and clicking on the links, find in the documentation that System.out.println(object) displays what the toString method of the object class returns.
Modify the display() method to use toString().
Check the correct operation of the method by launching the execution of the TestBook class.
Two TestBook programs for testing both questions a) and b) are enclosed. You should get the following outputs.
/*Example Output 6a*/
Book[title=Abstraction and Design Using Java, author=E.B.Koffman] Book@15ec28b2
/*Output 6b*/
Book[title=Data Structures in Java for Principled Programmer, author=Duane A.Bailey] Book[title=Abstraction and Design Using Java, author=E.B.Koffman]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
