8.2 Time Class Case Study

Our first example consists of two classes—Time1 (Fig. 8.1) and Time1Test (Fig. 8.2). Class Time1 represents the time of day. Class Time1Test’s main method creates one object of class Time1 and invokes its methods. The output of this program appears in Fig. 8.2.

 1   // Fig. 8.1: Time1.java 2   // Time1 class declaration maintains the time in 24-hour format. 3  4   public class Time1 5   { 6      private int hour; // 0 - 23   7      private int minute; // 0 - 59  8      private int second; // 0 - 59  9 10      // set a new time value using universal time; throw an11      // exception if the hour, minute or second is invalid12      public void setTime(int hour, int minute, int second)13 ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.