|
| ||
|
|
Start of topic | Skip to actions
Lab 10 Java Package; DrJava Project UtilityJava PackageA 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 asutility 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: DrJava language levels do not support packaging. We must use Full Java to work with packages.
Exercises:
/**
* Abstract list structure.
*/
public abstract class IntList {
public abstract Object accept(IntListVisitor v);
public ConsIntList cons(int n) {
return new ConsIntList(n, this);
}
}
/**
* Concrete empty list structure containing nothing.
*/
public class EmptyIntList extends IntList {
public static final EmptyIntList ONLY = new EmptyIntList();
private EmptyIntList() {
}
public Object accept(IntListVisitor v) {
return v.forEmptyIntList(this);
}
}
/**
* Concrete non-empty list structure containing an int, called first,
* and a rest, which is a list structure.
*/
public class ConsIntList extends IntList {
private int first;
private IntList rest;
/* NOTE: Programmer must write constructor code and gettors code in full Java.
*/
public ConsIntList(int f, IntList r) {
first = f;
rest = r;
}
public int first() {
return first;
}
public IntList rest() {
return rest;
}
public Object accept(IntListVisitor v) {
return v.forConsIntList(this);
}
}
/**
* Abstract operation on IntList.
*/
public interface IntListVisitor {
public Object forEmptyIntList(EmptyIntList host);
public Object forConsIntList(ConsIntList host);
}
funList and move IntList.java into it.
Reopen the file in the funList subdirectory. Now compile again. You should get no error this time.
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.
accept method of EmptyIntList. What can we do here?
Also, 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. 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.
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.
DrJava ProjectDrJava provides a utility called Project as a way to manage large Java programs with many files and many packages. We will illustrate the use of DrJava Project by writing a few visitors for thefunlist 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.
/**
* Computes the length of the host list using a tail-recursive
* helper visitor.
*/
public class GetLength implements IntListVisitor {
static GetLength ONLY = new GetLength();
private GetLength() {
}
/**
* @return an Integer
*/
Object forEmptyIntList(EmptyIntList host) {
return 0;
}
/**
* @return an Integer
*/
Object forConsIntList(ConsIntList host) {
return host.rest().accept(new GetLengthHelp(1));
}
}
class GetLengthHelp implements IntListVisitor {
int acc;
// need constructor
Object forEmptyIntList(EmptyIntList host) {
return acc ;
}
Object forConsIntList(ConsIntList host) {
return host.rest().accept(new GetLengthHelp(acc + 1));
}
}
Save it in a subdirectory of funlist called visitor. Now try to compile it! You will see a few error messages. Fix the errors until everything compiles.
Access Permissions: (Please don't edit)
Topic Actions: Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r9 < r8 < r7 < r6 < r5 | More topic actions
Webs: Main | TWiki | Africa | CPSX | EmbeddedSystems | Gpce | Houston | International | K12 | MetaOCaml | MulticoreOCR | ProgrammingLanguages | RAP | RIDL | Sandbox | SpeechClub | Teaching | Texbot | WG211 Web Actions: | |
This work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.