OOP 10. Exercises
Programming exercises
Enhance the TimeOfDay2 class by writing some of the following methods.
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()
incHour()
. Write a method that adds one to the hour field (increments). If the hour goes over 23, it should wrap around to 0.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 onincHour
naturally.compareTo(...)
. Write ancompareTo
method that returns true if the times are equal. The method header should look like this.public boolean equals(TimeOfDay t2)
- 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
- 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.
- __TimeOfDay2__ __7__ Class name. (sample answer - TimeOfDay2 is a class name)
- ______________ _____ Instance method name.
- ______________ _____ Static method name.
- ______________ _____ Constructor name.
- ______________ _____ Local variable name.
- ______________ _____ Instance variable name.
- ______________ _____ Static variable name.
- ______________ _____ Formal parameter name.
- ______________ _____ Actual argument expression.
- ______________ _____ Name of variable containing an object value.
- ______________ _____ Name of variable containing a primitive value.