Getter and setter methods

Figure 215. Direct access vs. setter method Slide presentation
Direct access Setter access
public class Time {
    public int hour, minute;
}
Time time = new Time();
    
time.hour = 17;
time.minute = 45;
public class Time {
    private int hour, minute;
    public void setTime(int h, int m) {
        minute = m;
        hour = h;
    }
}
Time time = new Time();
time.setTime(17, 45);

Figure 216. Why adding setter methods? Slide presentation
  • Allow for change of implementation.

  • Allow for non-business logic concerns. Examples:

    • Logging

    • Adding persistence (Databases)

public class Time {
    private int hour, minute;
    public void setTime(int h, int m) {
        minute = m;
        hour = h;
        System.out.println("Time has been set to " 
           + hour + ":" + minute);
    }
}

Figure 217. Implementation change: Minutes only, no hours Slide presentation
Direct access Setter / getter access
public class Time {
  // Minutes since 00:00
  public int minute;
}
final Time time = new Time();
time.minute = 17 * 60 + 45;
public class Time {
  private int minute;   // Minutes since 00:00
  public void set(int h, int m) {
    minute = m + 60 * h;
  }
  public int getMinute(){/* coming soon */}
  public int getHour(){/* coming soon */}
}
final Time time = new Time();
time.setTime(17, 45);

exercise No. 93

Implementing getter methods

Q:

We reconsider the setter / getter implementation in Figure 217, “Implementation change: Minutes only, no hours ”:

public class Time {
    private int minute;   // Minutes since 00:00

    public void set(int h, int m) {
        minute = m + 60 * h;
    }

    /**
     * Getting the current time's minute portion. Example: At 17:45 a value of
     * 45 will be returned.
     *
     * @return The current time's minute portion.
     */
    public int getMinute(){
        ... // implement me
        return ...;
    }

    /**
     * Getting the current time's hour portion. Example: At 17:45 a value of
     * 17 will be returned.
     *
     * @return The current time's hour portion.
     */
    public int getHour(){
        ... // implement me
        return ...;
    }
}

Complete the two getter methods' implementation.

Tip

A day's duration is 24 * 60 == 1440 minutes. The private variable minute thus ranges from 0 to 1439 minutes corresponding to 0:00 and 23:59.

A:

package access.by.setter;

public class Time {
    private int minute;   // Minutes since 00:00
    public void set(int h, int m) {
        minute = m + 60 * h;
    }

    /**
     * Getting the current time's minute portion. Example: At 17:45 a value of
     * 45 will be returned.
     *
     * @return The current time's minute portion.
     */
    public int getMinute(){
        return minute % 60; // 1 hour equals 60 minutes
    }

    /**
     * Getting the current time's hour portion. Example: At 17:45 a value of
     * 17 will be returned.
     *
     * @return The current time's hour portion.
     */
    public int getHour(){
        return minute / 60; // 1 hour equals 60 minutes
    }
}