OOP 9. toString() - TimeOfDay

Purpose of this lesson:

The toString instance method

A common method to write is toString, which returns a string representation of an object. This is convenient for displaying the value of an object for either debugging or for console IO.

String concatenation of objects. If you try to convert an arbitrary object to a String, as when you concatenate anything with a String, Java calls the object's toString method. If you didn't define this method, then your class inherited toString from the Object class. Object's version of toString doesn't produce anything very useful -- it gives the name of the class and the hexadecimal location in memory, for example, "TimeOfDay@82ba41".

TimeOfDay2 with toString method

This class is the same as in the previous example, except that the toString method has been defined.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
// File   : oop/timeofday/TimeOfDay2.java
// Purpose: A 24 hour time-of-day class to demo intro OOP concepts.
// Author : Fred Swartz - 2006-09-18 - Placed in public domain.
// Issues : Add a toString() method.

public class TimeOfDay2 {
    //========================================= instance variables
    private int _hour;
    private int _minute;

    //================================================ constructor
    public TimeOfDay2(int h, int m) {
        setHour(h);
        setMinute(m);
    }

    //================================================== getHour
    public int getHour() {
        return _hour;
    }

    //================================================== setHour
    public void setHour(int h) {
        if (h < 0 || h > 23) {
            throw new IllegalArgumentException(
                "TimeOfDay setHour: Bad hour value: " + h);
        }
        _hour = h;
    }

    //================================================ getMinute
    public int getMinute() {
        return _minute;
    }

    //================================================ setMinute
    public void setMinute(int m) {
        if (m < 0 || m > 23) {
            throw new IllegalArgumentException(
                "TimeOfDay setMinute: Bad minute value: " + m);
        }
        _minute = m;
    }
    //================================================= toString
    @Override public String toString() {
        return _hour + ":" + _minute;
    }
}

Test program using toString

Note how convenient the output is.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
// File   : oop/timeofday/TimeTest2.java
// Purpose: Test the TimeOfDay2 class.
//          toString called automatically for concatenation.
// Author : Fred Swartz
// Date   : 2005-04-30

import javax.swing.*;

public class TimeTest2 {
    public static void main(String[] args) {

        TimeOfDay2 then = new TimeOfDay2(8, 35);
        TimeOfDay2 now  = new TimeOfDay2(14, 5);

        JOptionPane.showMessageDialog(null, "From " + then + " to " + now);
    }
}

Exercises

  1. Two digit times We normally expect to see time values expressed with two digits, but the version of toString above formats numbers less than 10 as single digits, eg 12:7 altho we would expect to see 12:07. Change toString to do this. Altho a good way to do this would be to use DecimalFormat or the newer String.format to accomplish this, try it using only if statements and string operations for practice.
  2. toString12(). Write a method, toString12, that returns the time as a string in 12-hour form, appending AM or PM as appropriate. For example, for the time 22:37 would be returned as "10:37 AM". There are three cases:
    • Before 12 is AM.
    • Between 12 and 13 is PM, but written with 12.
    • 13 and after are PM with 12 subtracted from the hour.