Homework 3
Understanding class definitions  

powered by FreeFind

Modified: 

SI - Next session will give additional discussion of Homework 3.

Overview - The purpose of the homework is to review Chapter 2 concepts and additional practice skills needed throughout the course. 

Assignment

  1. Exercises 2.64-2.71 of text, pages 47 through 49.
    1. From the View menu, Show Terminal, then Record method calls. Clear the window before starting.
    2. Exercise 2.64
      • A Book class can be found in the book-exercise project for Chapter 2. Two fields and a constructor to initialize the fields have been defined. In this exercise and the next few, you will add further features to the Book class outline.
      • Add two accessor methods to the class - getAuthor and getTitle - that return the author and title field values as their respective results. Test your class by creating some instances and calling the methods.

       

    3. Exercise 2.65
      • Add two methods, printAuthor and printTitle, to the Book class. These should print the author and title respectively, to the terminal window.

      After completing Exercise 2.64 and 2.65.

      1. Create a Book object with author and title.
      2. Call your printAuthor and printTitle methods.

       

    4. Exercise 2.66
      • Add a field, pages, to the Book class to store the number of pages.
      • Modify the constructor to initialize the author, title and pages fields to a value passed to the constructor.
      • Add an appropriate getPages accessor method.

      After completing Exercise 2.66.

      1. Create a Book object with author, title and pages using your Book constructor.
      2. Call your getPages accessor method.

       

    5. Exercise 2.67
      • Add a method printDetails to the Book class. The method should print author, title, and pages fields to the terminal window. Follow the example below:

      Title: Robinson Crusoe, Author: Daniel Defoe, Pages: 232

      After completing Exercise 2.67.

      1. Create a Book object with author, title and pages using your Book constructor.
      2. Call your printDetails method.

       

    6. Exercise 2.68
      • Add another field named refNumber to the Book class. The field stores a library reference number as a String and is initialized to a zero length string (i.e. ""). Modify the constructor of Exercise 2.66 to initialize the field, it does not need to be passed another parameter.
      • Add a mutator method with the following signature:

        public void setRefNumber( String ref )

      The body should assign the value of the parameter to the refNumber field.

      • Add an accessor method getRefNumber that returns the value of to the refNumber field.

      After completing Exercise 2.68.

      1. Create a Book object with author, title and pages using your Book constructor.
      2. Call your setRefNumber mutator method with the parameter "QF 742.3".
      3. Call your getRefNumber accessor method.

       

    7. Exercise 2.69
      • Modify your printDetails method to include printing the reference number. However, the reference number should be printed only if it is non-blank (i.e. not zero length). If it is zero length, print "ZZZ" instead.

      Hint: Use a conditional statement that tests the refNumber length to be greater than 0.

      After completing Exercise 2.69.

      1. Create a Book object with author, title and pages using your Book constructor.
      2. Call your printDetails method.

       

    8. Exercise 2.70
      • Modify the setRefNumber mutator so that it sets refNumber only if the parameter string is length 3 or greater. If less than 3, print an error message and leave the field unchanged.

      After completing Exercise 2.70.

      1. Create a Book object with author, title and pages using your Book constructor.
      2. Call your setRefNumber mutator method with the parameter "QF 742.3".
      3. Call your setRefNumber mutator method again with the parameter "12".
      4. Call your getRefNumber accessor method.

       

    9. Exercise 2.71
      • Add an integer field, borrowed, to Book class. This keeps count of the number of times a book has been borrowed.
      • Add a mutator borrow that adds 1 to the borrowed field each time it is called.
      • Add an accessor getBorrowed that returns the value of the borrowed field.
      • Modify printDetails so that it includes this field with an explanatory piece of information.

      After completing Exercise 2.71.

      1. Create a Book object with author, title and pages using your Book constructor.
      2. Call your borrow mutator method 3 times.
      3. Call your getBorrowed accessor method.
      4. Call your printDetails method.

       

    10. Print the terminal window by:
      1. Open terminal window.
      2. Copy the terminal window to the clipboard.
      3. Open Word.
      4. Type in your name and Assignment 1.
      5. Paste clipboard into Word.
      6. Print.
    11. Print the complete Book class source code.

     

  2. Exercises 2.72 and 2.73 of text, page 49.
    1. From the View menu, Show Terminal, then Record method calls. Clear the window before starting.
    2. Exercise 2.72
      • Create a new project named heater exercise.
      • Create a new class named Heater.
      • Add a field named temperature that is an integer.
      • Add a constructor with no parameters that sets temperature to 15.
      • Add mutators warmer and cooler that increase and decrease the temperature field by 5 respectively.
      • Add accessor method to return the value of temperature.

      After completing Exercise 2.72.

      1. Create a Heater object.
      2. Call warmer and cooler mutator methods each one time.
      3. Call your accessor method for temperature.

       

    3. Exercise 2.73
      • Modify your Heater class by adding integer field increment.
      • Modify the constructor to initialize increment to 5.
      • Modify warmer and cooler mutator methods to use the increment field rather than 5.
      • Add a method setIncrement that takes one integer parameter and assigns that value to increment field. If the parameter is negative do not assign to increment.

      After completing Exercise 2.73

      1. Call setIncrement mutator method with the parameter of 3.
      2. Call warmer mutator method one time.
      3. Call cooler mutator method two times.
      4. Call your accessor method for temperature.
      5. Call cooler mutator method two times.
      6. Call your accessor method for temperature.
      7. Call setIncrement mutator method with the parameter of -1.
      8. Call warmer mutator method one time.
      9. Call your accessor method for temperature.
      10. Print the terminal window by:
        1. Open terminal window.
        2. Copy the terminal window to the clipboard.
        3. Open Word.
        4. Type in your name and Assignment 2.
        5. Paste clipboard into Word.
      11. Inspect and print the Heater object by:
        1. Right-click on the object.
        2. Choose Inspect.
        3. Press Alt Prnt Scrn keys simultaneously. The screen will be written to the clipboard.
        4. Paste the picture into Word.
        5. Print.
    4. Print the complete Heater class source code.

Turn in

  1. First Page - Contains your name, date and Homework 3. Staple all pages together.
  2. Class and Screen printouts - Assignments 1 and 2.