I am doing an exercise on reusable classes, but I can't seem to get it to work right. Here is the code
Time3.java
//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
and its tester
//Jeremiah A. Walker /** Once it's compiled and stored in its package, the class can be imported into programs. In this Tester, we specify that class Time3 should be imported for use in class Time3Test. This class is in the default package because its .java file does not contain a package declaration. Since the classes are in different package, the import at the beginninf is required so that our tester can use Time3. **/ import com.walker.jhtp.reucla.Time3;//import class Time 3 public class Time3Test { public static void main(String[] args) { //Create and initialize a Time3 object Time3 time = new Time3();//invokes Time3 constructor //output string representation of the time System.out.print("The initial universal time is: "); System.out.println(time.toUniversalString()); System.out.print("The initial standard time is: "); System.out.println(time.toString()); System.out.println();//output a blank line //change time and output updated time time.setTime(13, 14, 15); System.out.print("The initial universal time is: "); System.out.println(time.toUniversalString()); System.out.print("The initial standard time is: "); System.out.println(time.toString()); System.out.println();//output a blank line //attempt to set time with invalid values try { time.setTime(99,99,99); //all values out of range }//end try catch (IllegalArgumentException e) { System.out.printf("Exception: %s\n\n", e.getMessage()); }//end catch //display time after attempt to set invalid values System.out.println("After attempting invalid settings: "); System.out.print("The initial universal time is: "); System.out.println(time.toUniversalString()); System.out.print("The initial standard time is: "); System.out.println(time.toString()); System.out.println();//output a blank line }//end main }//end class Time3Test
I am following the directions to the letter, but I am getting an error saying that the directory doesn't exist. What am I doing wrong.