Lab 10 Java Packages, Generics; 

Java Packages

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 funList , you must add the declaration package funList ; 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 funList should be in a directory also named funList. If you don't do this, it will still compile, but it won't run correctly.

NOTE: DrJavalanguage levels do not support packaging. We must use Full Java to work with packages.

Exercises:

You need to create a subdirectory called funList and move IntList.java into it.  The full class name for IntList is now funlist.IntList.

Reopen the file in the funList subdirectory. Now compile again. You should get error messages saying  it can't find class IntListVisitor and class ConsIntList this time.  You will need to package all the other classes/intefaces and move them into appropriate subdirectories.

Note: if you use the command window to compile with the command javac, 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 code to test the accept method of EmptyIntList . What can we do here?

If you try to compile TestEmptyIntList.java now, you will get an error message. Try it to see what happens.

You need to add the statement import funList.* ; to the top of TestEmptyIntList.java to indicate to the compiler that you are using all the public classes in that package. Try to compile it again.  Is everything OK?

Now, remove the public access from the EmptyIntList class. By default, a class is "package-private", i.e., it is known within the package, but not from outside.  Try to compile again.  You should see a few error messages saying that you can't use EmptyIntList.java because it is not public. This is because the TestEmptyIntList class is not part of the funList package. One way to resolve this problem by making TestEmptyIntList part of the funList package. A class of a package can access all the classes (public or "package-private") in the package. However this is not a good solution in general because a client may be using many classes from different packages, but no class can be part of more than one package. For now, just make EmptyIntList.java public again, and recompile TestEmptyIntList.java . You should get no error. Try to run Test_List.java now by click the Test button in DrJava.

Java Generics

Please refer to the Lecture 30 notes.   Also, download and open the contained DrJava project in lec29_code.zip .

Practice writing some visitors using generics:

  1. Forward accumulation sum of a list of integers.
  2. Concatenate, with spaces in between, all the elements of a list of Strings.
  3. A single ToString visitor class that could be used to create instances that could then be used on any given type of list.

DrJava Projects

Here's some more information on using DrJava's project capabiltiies:
DrJavaprovides a utility called Project as a way to manage large Java programs with many files and many packages. We will illustrate the use of DrJavaProject by writing a few visitors for the funlist package.

In general, a project has a bunch of java files (the source code) and class files. It is a good idea to separate the java files from the compiled class files by creating a subdirectory called bin for the class files and src for the java files.

Set the project root directory lab10, the build directory to lab10/ bin and working directory to lab10/ src . Click OK and now we have an empty project file called {{ListVisProj.drjava }} saved in xml format. Take a look at the subdirectory lab10 to see what's there.

Now let's write a list visitor to compute the length of a list and add it to the project.

Save it in a subdirectory of funlist called visitor. The full class name for GetLength and GetLengthHelp are now funlist.visitor.GetLength and funlist.visitor.GetLengthHelp, respectively. Note that GetLength is public while GetLengthHelp is package private. Now try to compile it! You will see a few error messages. Fix the errors until everything compiles.

Be sure to save the project code periodically.