Problem:
Write a class named Book
that keeps track of book objects such that the instance data contains the
book’s title and author and a unique
identification number, say id that starts form 1 for the
first book object and is incremented by 1 each time a new book with at least
the title specified is created. The required methods in the Book class are as
follows:
- 3 constructors:
One without parameters that sets the title and author to “unknown” and id
to 0; one with a single parameter that sets the title to the given value
as parameter, sets the author to “unknown”, increments the sequence by 1
and sets id to this sequence number; one with two parameters for title and
author, setting the corresponding instance data, and id as in the second
constructor.
- The setter
methods for title and author.
- The getter
methods for title, author, and id.
- The equals
method that compares the current book object with another Book object
given as a parameter and returns true if both objects have the same title
and author properties, and false otherwise.
- The toString
method that returns a text including the book’s title, author and id.
Refer to the sample execution window for the test program for details.
- The getInitials
method that returns the initial letters of the author’s first name(s) and
last name, if the author’s name is known (not equal to “unknown”, or not
null).Assume that there can be at most two names and one surname separated
by a single blank. Refer to the sample execution window for the test
program for details.
Write a driver class
that tests the Book class. Allow the user to enter as many book objects as s/he
wants. Take care of necessary object initializations. Store the first and last
book objects separately. Print each book object. Compare the first and last
book objects, if any, and display a proper message if they are the same.
Display the last book’s author’s initials, if the user has input any valid book
object.
Sample execution windows:
To end the input process bypass each question by typing the enter key!
Type the title of the book:
Type the name of the author:
Press any key to continue...
To end the input process bypass each question by typing the enter key!
Type the title of the book: Windows NT Server 4.0
Type the name of the author: Russel
Book No: 1 entitled "Windows NT Server 4.0" written by Russel
Type the title of the book:
Type the name of the author:
First and last books are same
Last book's author has the initials, R.
Press any key to continue...
To end the input process bypass each question by typing the enter key!
Type the title of the book: Java Software Solutions
Type the name of the author: Lewis Loftus
Book No: 1 entitled "Java Software Solutions" written by Lewis
Loftus
Type the title of the book: Introduction to Java Programming with
JBuilder
Type the name of the author: Yvet Daniel Liang
Book No: 2 entitled "Introduction to Java Programming with
JBuilder" written by
Yvet Daniel Liang
Type the title of the book:
Type the name of the author:
Last book's author has the initials, Y.D.L.
Press any key to continue...
Solution: