- If you haven’t completed the previous exercises in this chapter, complete them now before continuing.
- 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);
}
- 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);
}
}
- Compile and run the program.
$ javac DisplayingObjects.java
$ java DisplayingObjects
22:11:22
- Add the following line to the printTime method.
System.out.printf("%02d:%02d:%04.1f\n", t.getHour(), t.getMinute(),
(float)t.getSecond());
- Compile and run the program.
$ javac DisplayingObjects.java
$ java DisplayingObjects
22:11:22
22:11:22.0