Java Reference
In-Depth Information
Lab 7.1: Working with Packages
The purpose of this lab is to become familiar with writing, compiling,
and running Java classes that appear in packages.
1. After completing Lab 6.2, you had written four classes: Polygon,
Triangle, RightTriangle, and a class with main() in it that you exe-
cuted to test the other three classes. Copy and paste the four source
code files (the *.java files) for these four classes into a directory
named c:\src (or similar folder).
2. Add the Polygon, Triangle, and RightTriangle classes to the geometry
.shapes package, and compile these classes using the -d flag. The
output directory can be any folder you choose, or you can use the
c:\my_bytecode folder from the example in this chapter.
3. Add the fourth class (the program you wrote) in a package named
geometry.programs. Compile this class using the -d flag.
4. Set your classpath to include the directory where the compiler out-
put the bytecode.
5. Run the program again.
The output of running your program will be the same. However, you
now need to include the package name when running the program from
the DOS prompt, and you should be able to run the program from any
directory at the DOS prompt.
Lab 7.2: Using Encapsulation
In this lab you will write a class that takes advantage of the benefits of
encapsulation.
1. Declare a class named Television. Add two fields: channel and vol-
ume. Declare both fields as private.
2. Add accessor and mutator methods for both fields. The valid values
for volume are 0 to 10, so disregard any attempts to set the volume
to a value outside that range. Similarly, the channel field should only
be set to a value between 2 and 999.
Search WWH ::




Custom Search