//Jeremiah A.Walker
/**
Steps for Declaring a Reusable Class
Before a class can be imported into multiple applications, it must be placed in a package
to make it reusable. Figure 8.15 shows how to specify the package in which a class should
be placed. Figure 8.16 shows how to import our packaged class so that it can be used in
an application. The steps for creating a reusable class are:
1. Declare a public class. If the class is not public, it can be used only by other
classes in the same package.
2. Choose a unique package name and add a package declaration to the source-code
file for the reusable class declaration. In each Java source-code file there can be
only one package declaration, and it must precede all other declarations and
statements. Comments are not statements, so comments can be placed before a
package statement in a file. [Note: If no package statement is provided, the class
is placed in the so-called default package and is accessible only to other classes in
the default package that are located in the same directory. All prior programs in
this book having two or more classes have used this default package.]
3. Compile the class so that it’s placed in the appropriate package directory.
4. Import the reusable class into a program and use the class.
We’ll now discuss each of these steps in detail.**/
//Time3 class declaration maintains the time in 24 format.
package com.walker.jhtp.reucla;
public class Time3
{
private int hour; //0 - 23
private int minute; //0 - 59
private int second; //0 - 59
/**
Setting a new time value using universal time;
This will throw an exception if the hour, minute or second is invalid
**/
public void setTime(int h, int m, int s)
{
//validate hour, minute and second
if((h >= 0 && h < 24) && (m >= 0 && m < 60 ) && (s >= 0 && s < 60) )
{
hour = h;
minute = m;
second = s;
}//end if
else
throw new IllegalArgumentException("Hour, minute, and/or second was out of range");
}//end setTime
//convert to String in universal-time format (hh:mm:ss)
public String toUniversalString()
{
return String.format("%02d:%02d:%02d", hour, minute, second);
}//end method toUniversalString
//convert to String in stangdard-time format (H:MM:SS AM or PM)
public String toString()
{
return String.format("%d:%02:%02d %s", ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? "AM" : "PM"));
}//end method toString
}//end class Time3