|
OFWB - Ch2 - Book-exercise project
Page history
last edited
by Steve Sweeney 13 years, 6 months ago
The following questions relate to the book-exercise project at the end of chapter 2, "Objects First with Java".
- Add two accessor methods to the class - getAuthor and getTitle - that return the author and the title fields as their respective results. Test your class by creating some instances and calling the methods. (Solution)
- Add two methods, printAuthor and printTitle, to the outline Book class. These should print the author and title fields, respectively, to the terminal window. (Solution)
- Add a further 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 contstructor, along with the author and title strings. Include an appropriate getPages accessor method for this field. (Solution)
- 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 formatted. (Solution)
- 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 signature:
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 correctly. (Solution)
- Modify your printDetails method to include printing the reference number. 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. (Solution)
- 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. (Solution)
- 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. (Solution)
OFWB - Ch2 - Book-exercise project
|
Tip: To turn text into a link, highlight the text, then click on a page or file from the list above.
|
|
|
Comments (0)
You don't have permission to comment on this page.