Java Reference
In-Depth Information
Exercise 2.84 Add two methods, printAuthor and printTitle , to the outline Book
class. These should print the author and title fields, respectively, to the terminal window.
Exercise 2.85 Add a field, pages , to the Book class to store the number of pages. This
should be of type int , and its initial value should be passed to the single constructor, along
with the author and title strings. Include an appropriate getPages accessor method for
this field.
Exercise 2.X Are the Book objects you have implemented immutable? Justify your answer.
Exercise 2.86 Add a method, printDetails , to the Book class. This should print details
of the author, title, and pages to the terminal window. It is your choice how the details are for-
matted. For instance, all three items could be printed on a single line, or each could be printed
on a separate line. You might also choose to include some explanatory text to help a user
work out which is the author and which is the title, for example
Title: Robinson Crusoe, Author: Daniel Defoe, Pages: 232
Exercise 2.87 Add a further field, refNumber , to the Book class. This field can store a
reference number for a library, for example. It should be of type String and initialized to the
zero length string ( "" ) in the constructor, as its initial value is not passed in a parameter to the
constructor. Instead, define a mutator for it with the following header:
public void setRefNumber(String ref)
The body of this method should assign the value of the parameter to the refNumber field.
Add a corresponding getRefNumber accessor to help you check that the mutator works
Exercise 2.88 Modify your printDetails method to include printing the reference num-
ber. However, the method should print the reference number only if it has been set—that is,
the refNumber string has a non-zero length. If it has not been set, then print the string "ZZZ"
instead. Hint: Use a conditional statement whose test calls the length method on the ref-
Number string.
Exercise 2.89 Modify your setRefNumber mutator so that it sets the refNumber field
only if the parameter is a string of at least three characters. If it is less than three, then print an
error message and leave the field unchanged.
Exercise 2.90 Add a further integer field, borrowed , to the Book class. This keeps a count
of the number of times a book has been borrowed. Add a mutator, borrow , to the class. This
should update the field by 1 each time it is called. Include an accessor, getBorrowed , that
returns the value of this new field as its result. Modify printDetails so that it includes the
value of this field with an explanatory piece of text.
Exercise 2.91 Add a further boolean field, courseText , to the Book class. This re-
cords whether or not a book is being used as a text book on a course. The field should be
Search WWH ::

Custom Search