This tutorial covers:
Create a subdirectory for this lab, say lab03, and click here to download the jar files listFWSource.jar and CLFactory.jar into this sub directory.
listFWSource.jar contains the code for the list framework modified for the purpose of this lab only. CLFactory.jar contains the binary class file for theCompositeListFactory
discussed in the
lecture.
The Java Development Kit (JDK) comes with a tool called JavaDoc. This tool will generate documentation for Java source code with comments written in accordance with the Java documentation style. The following links show more examples. You do need to spend time outside of the lab to study them.
Extract the files from listFWSource.jar using the command:jar -xf listFWSource.jar
We will provide a more detailed discussion of "jar" files in another lab.
Use Emacs to look at both the code and the comments, which follow the Javadoc conventions. The following is a very short summary of the Javadoc conventions.
Exercises:
Many html files will be generated. Use a browser to view index.html. Does the documentation include descriptions of the private fields and methods? Now execute the command
javadoc -private *.java
What is the difference? How about
javadoc
-private -author *.java
Now try simply
javadoc
Like many UNIX commands, running the program without specifying any options will provide a brief description of its possible usage.
Take a brief look at the explanation of the flags.
In particular, note the
Remove all files generated by javadoc. Be sure not to remove any of the java files. We will generate the javadoc files again, after we edit and re-package the java files as prescribed in the following subsection.
A Java package is a grouping of classes similar to the notion of a
directory is a grouping of files. Packages are used to help avoid
name clashes and to hide particular pieces of code from the clients.
A package has a name, such as utility
or
java.lang.util
.
In general, a package name is a series of strings of alphanumeric
characters (starting with an alphabetic character)
and separated by periods. To make a java class part of a particular
package, say listFW
, you must
add the declaration package listFW;
to the very top
of the class source file.
Also, you will need to put the file in the directory structure that
mirrors the package name. For example, the java classes that belong
to the package scheme should be in a directory also named listFW
.
If you don't do this, it will still compile, but it won't run correctly.
Exercises:
Make IList
part of a package called listFW
by adding the package listFW;
declaration to the top of IList.java
.
You need to create a subdirectory called listFW
and
move IList.java
into it.
Now compile using
javac listFW/IList.javaNote that you should always compile from your project's main directory. If you compile from within a package subdirectory, it doesn't find all the supporting definitions.
We can't run anything yet, because that's just a piece of the whole program.
Add the package listFW;
declaration to the top of IEmptyList.java
and
INEList.java
,
and move them into the listFW
subdirectory.
Do not make GetLength.java
and TestLength.java
part of
the package. GetLength.java
and TestLength.java
do not have a package name, and they are thus said to be in the no-name (or default)
package.
If you try to compile GetLength.java
now, you will get an error message. Try it to see what happens.
Since GetLength
makes use of IEmptyList
and INEList
that are in the listFW
package, you need to add the statement import listFW.*;
to the
top of GetLength.java
to indicate to the compiler
that you are using all the public
classes in the listFW
package.
Try to compile it again.
GetLength
is an algorithm on IList
.
We want to put it in a package that is easy to locate. A reasonable
place to put it is in a subdirectory of listFW
.
Create a
subdirectory of listFW
called visitor
, and move GetLength.java
to this subdirectory. The package name corresponding to this
subdirectory is listFW.visitor
.
Add the package listFW.visitor;
declaration to the top of
GetLength.java
.
In order to compile GetLength.java
, you need to go back to
the directory that contains listFW as a subdirectory. To avoid confusion,
remove the binary class file GetLength.class
from the current directory.
Now compile GetLength.java
with the command
javac listFW/visitor/GetLength.java
To test GetLength, you need to have way to manufacture concrete IList objects. The class CompositeListFactory discussed in the lecture can manufacture IEmptyList and INEList objects. Take a look at the main method and the run method of the test program, TestLength.java. As you can see, it makes use of CompositeListFactory to create IList objects and compute their lengths to test GetLength. The binary class file for CompositeListFactory is bundled into the jar file CLFactory.jar that you downloaded earlier.
To compile your test code, type the following (Unix) command from your current lab directory:
javac -classpath .:CLFactory.jar TestLength.java
You should see a few error messages. What are they? You need to
import listFW.*, to make use of all the classes for the list framework,
import listFW.visitor.*, to make use of the GetLength algorithm,
Now, edit the file
GetLength.java
and add the
keyword public in front of the definition class
GetLength
.
Without the public access specifier,
GetLength
is
said to be package-private and can only be used by classes inside
of the listFW.visitor
package.
import listFW.factory.*, to make use of the CompositeListFactory.
Recompile TestLength.java
. You should
get no error.
To run your test code, type the following (Unix) command from your current lab directory:
java -classpath .:CLFactory.jar TestLength
javadoc -private -author listFW/*.java listFW/visitor/*.java
In the following exercises, assume
INEList contains Integer objects only. (Recall Integer is the wrapper class for int as discussed in class.) Package all algorithms inlistFW.visitor
.
Sum
to compute the sum of the elements in
a host list. Write one version using direct recursion, and one
version, called GetSum
, using a helper alogorithm called HelpGetSum
.
Write a simple test class.