google search

Custom Search

Friday, August 8, 2008

Summary of Creating and Using Packages and Questions and Exercises: Creating and Using Packages

To create a package for a type, put a package statement as the first statement in the source file that contains the type (class, interface, enumeration, or annotation type).

To use a public type that's in a different package, you have three choices: (1) use the fully qualified name of the type, (2) import the type, or (3) import the entire package of which the type is a member.

The path names for a package's source and class files mirror the name of the package.

You might have to set your CLASSPATH so that the compiler and the JVM can find the .class files for your types.
Questions

Assume you have written some classes. Belatedly, you decide they should be split into three packages, as listed in the following table. Furthermore, assume the classes are currently in the default package (they have no package statements).

Destination Packages

Package Name


Class Name

mygame.server


Server

mygame.shared


Utilities

mygame.client


Client

1. Which line of code will you need to add to each source file to put each class in the right package?

2. To adhere to the directory structure, you will need to create some subdirectories in the development directory and put source files in the correct subdirectories. What subdirectories must you create? Which subdirectory does each source file go in?

3. Do you think you'll need to make any other changes to the source files to make them compile correctly? If so, what?

Exercises

Download the source files as listed here.

* Client
* Server
* Utilities

1. Implement the changes you proposed in questions 1 through 3 using the source files you just downloaded.

2. Compile the revised source files. (Hint: If you're invoking the compiler from the command line (as opposed to using a builder), invoke the compiler from the directory that contains the mygame directory you just created.)


Questions

Assume you have written some classes. Belatedly, you decide they should be split into three packages, as listed in the following table. Furthermore, assume the classes are currently in the default package (they have no package statements).

Destination Packages

Package Name


Class Name

mygame.server


Server

mygame.shared


Utilities

mygame.client


Client

1. Which line of code will you need to add to each source file to put each class in the right package?

2. To adhere to the directory structure, you will need to create some subdirectories in the development directory and put source files in the correct subdirectories. What subdirectories must you create? Which subdirectory does each source file go in?

3. Do you think you'll need to make any other changes to the source files to make them compile correctly? If so, what?

Exercises

Download the source files as listed here.

* Client
* Server
* Utilities

1. Implement the changes you proposed in questions 1 through 3 using the source files you just downloaded.

2. Compile the revised source files. (Hint: If you're invoking the compiler from the command line (as opposed to using a builder), invoke the compiler from the directory that contains the mygame directory you just created.)

0 comments:

 

blogger templates | Make Money Online