OOP 10. Exercises

Programming exercises

Enhance the TimeOfDay2 class by writing some of the following methods.

  1. isAM() Write a method, isAM, that returns a boolean true if the time is in the AM, and false in the PM. Assume that 12:00 is PM. The prototype should be:
       public boolean isAm()
  2. incHour(). Write a method that adds one to the hour field (increments). If the hour goes over 23, it should wrap around to 0.
  3. incMinute(). Write a void method that adds one to the minute field. If the minutes go over 59, the "carry" should go into the hour field (by calling on incHour naturally.
  4. compareTo(...). Write an compareTo method that returns true if the times are equal. The method header should look like this.
        public boolean equals(TimeOfDay t2)
  5. Change implementation without changing public interface. One reason to keep instance variables private is to allow implementation changes. Rewrite the class to use only one instance variable for minutes. There is no need to keep the hours because the hours can be calculated from the number of minutes. You can use division and mod in the getters. It would probably be most clear to have only one setter which takes two parameters, hours and minutes.

Terminology Exercises

  1. Give an example name matching the term and the line number from either of the two classes above (TimeOfDay2 or TimeTest2). If there is more than one, you only have to supply one. If there is none, then write "NONE" on the line. Most questions require a name; if a question requires an expression, that is specified.
    1. __TimeOfDay2__ __7__ Class name. (sample answer - TimeOfDay2 is a class name)
    2. ______________ _____ Instance method name.
    3. ______________ _____ Static method name.
    4. ______________ _____ Constructor name.
    5. ______________ _____ Local variable name.
    6. ______________ _____ Instance variable name.
    7. ______________ _____ Static variable name.
    8. ______________ _____ Formal parameter name.
    9. ______________ _____ Actual argument expression.
    10. ______________ _____ Name of variable containing an object value.
    11. ______________ _____ Name of variable containing a primitive value.