| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

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".

 

  1. 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)
  2. 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)
  3. 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)
  4. 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)
  5. 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)
  6. 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)
  7. 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)
  8. 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)

Comments (0)

You don't have permission to comment on this page.