Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

The Java class libraries are divided into packages of related classes and interfaces. Most Java programs you build reuse existing packages, such as the Java class libraries, third party libraries or libraries developed within your organization. This tutorial shows how create your own packages of classes for reuse or distribution. The tutorial is intended for students or professionals who are alreadyfamiliar with building Java classes.

Download the code for this tutorial here.

[Note: This tutorial is an excerpt (Section 8.16) of Chapter 8, Class and Objects: A Deeper Look, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.390-396. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

8.16 Time Class Case Study: Creating Packages (Continued)

Step 4: Importing the Reusable Class

Once the class is compiled and stored in its package, the class can be imported into programs (Step 4). In the Time1PackageTest application of Fig. 8.19, line 3 specifies that class Time1 should be imported for use in class Time1PackageTest. Class Time1PackageTest is in the default package because the class’s .java file does not contain a package declaration. Since the two classes are in different packages, the import at line 3 is required so that class Time1PackageTest can use class Time1.

   1  // Fig. 8.19: Time1PackageTest.java
2 // Time1 object used in an application.
3 import com.deitel.jhtp6.ch08.Time1; // import class Time1
4
5 public class Time1PackageTest
6 {
7 public static void main( String args[] )
8 {
9 // create and initialize a Time1 object
10 Time1 time = new Time1(); // calls Time1 constructor
11
12 // output string representations of the time
13 System.out.print( "The initial universal time is: " );
14 System.out.println( time.toUniversalString() );
15 System.out.print( "The initial standard time is: " );
16 System.out.println( time.toString() );
17 System.out.println(); // output a blank line
18
19 // change time and output updated time
20 time.setTime( 13, 27, 6 );
21 System.out.print( "Universal time after setTime is: " );
22 System.out.println( time.toUniversalString() );
23 System.out.print( "Standard time after setTime is: " );
24 System.out.println( time.toString() );
25 System.out.println(); // output a blank line
26
27 // set time with invalid values; output updated time
28 time.setTime( 99, 99, 99 );
29 System.out.println( "After attempting invalid settings:" );
30 System.out.print( "Universal time: " );
31 System.out.println( time.toUniversalString() );
32 System.out.print( "Standard time: " );
33 System.out.println( time.toString() );
34 } // end main
35 } // end class Time1PackageTest
 Fig. 8.19  Time1 object used in an application.


The initial universal time is: 00:00:00
The initial standard time is: 12:00:00 AM

Universal time after setTime is: 13:27:06
Standard time after setTime is: 1:27:06 PM

After attempting invalid settings:
Universal time: 00:00:00
Standard time: 12:00:00 AM

Page 1 | 2 | 3 | 4 | 5
Return to Tutorial Index