CH11 – Displaying Objects

  1. If you haven’t completed the previous exercises in this chapter, complete them now before continuing.
  2. Create a new class named DisplayingObjects and add the following method.
public static void printTime(Time t){
  System.out.print(t.hour);
  System.out.print(":");
  System.out.print(t.minute);
  System.out.print(":");
  System.out.print(t.seconds);
}
  1. Add a main method that uses TheTimeClass and calls printTime.
public class DisplayingObjects{
  public static void printTime(Time t){
    System.out.print(t.getHour());
    System.out.print(":");
    System.out.print(t.getMinute());
    System.out.print(":");
    System.out.print(t.getSecond() + "\n");
  }
  public static void main(String[] args){
    TheTimeClass myTime = new TheTimeClass(22,11,22);
    DisplayingObjects.printTime(myTime);
  }
}
  1. Compile and run the program.
$ javac DisplayingObjects.java
$ java DisplayingObjects
22:11:22
  1. Add the following line to the printTime method.
System.out.printf("%02d:%02d:%04.1f\n", t.getHour(), t.getMinute(),
 (float)t.getSecond());
  1. Compile and run the program.
$ javac DisplayingObjects.java
$ java DisplayingObjects
22:11:22
22:11:22.0