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)

Steps 1 and 2: Creating a public Class and Adding the package Statement

For Step 1, we modify the public class Time1 declared in Fig. 8.1. The new version is shown in Fig. 8.18. No modifications have been made to the implementation of the class, so we will not discuss its implementation details again here.

   1  // Fig. 8.18: Time1.java
2 // Time1 class declaration maintains the time in 24-hour format.
3 package com.deitel.sjhtp6.ch08;
4
5 public class Time1
6 {
7 private int hour; // 0 - 23
8 private int minute; // 0 - 59
9 private int second; // 0 - 59
10
11 // set a new time value using universal time; perform
12 // validity checks on the data; set invalid values to zero
13 public void setTime( int h, int m, int s )
14 {
15 hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); // validate hour
16 minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); // validate minute
17 second = ( ( s >= 0 && s < 60 ) ? s : 0 ); // validate second
18 } // end method setTime
19
20 // convert to String in universal-time format (HH:MM:SS)
21 public String toUniversalString()
22 {
23 return String.format( "%02d:%02d:%02d", hour, minute, second );
24 } // end method toUniversalString
25
26 // convert to String in standard-time format (H:MM:SS AM or PM)
27 public String toString()
28 {
29 return String.format( "%d:%02d:%02d %s",
30 ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
31 minute, second, ( hour < 12 ? "AM" : "PM" ) );
32 } // end method toString
33 } // end class Time1
 Fig. 8.18  Packaging class Time1 for reuse.


    For Step 2, we add a package declaration (line 3) that declares a package named com.deitel.sjhtp6.ch08. Placing a package declaration at the beginning of a Java source file indicates that the class declared in the file is part of the specified package. Only package declarations, import declarations and comments can appear outside the braces of a class declaration. A Java source-code file must have the following order:

1. a package declaration (if any),

2. import declarations (if any), then

3. class declarations.

Only one of the class declarations in a particular file can be public. Other classes in the file are placed in the package and can be used only by the other classes in the package. Non-public classes are in a package to support the reusable classes in the package.

     In an effort to provide unique names for every package, Sun Microsystems specifies a convention for package naming that all Java programmers should follow. Every package name should start with your Internet domain name in reverse order. For example, our domain name is deitel.com, so our package names begin with com.deitel. For the domain name yourcollege.edu, the package name should begin with edu.yourcollege. After the domain name is reversed, you can choose any other names you want for your package. If you are part of a company with many divisions or a university with many schools, you may want to use the name of your division or school as the next name in the package. We chose to use sjhtp6 as the next name in our package name to indicate that this class is from Small Java How to Program, Sixth Edition. The last name in our package name specifies that this package is for Chapter 8 (ch08).

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