Date: Thu, 28 Mar 2024 21:00:42 -0500 (CDT) Message-ID: <1189382523.1079.1711677642684@wiki-n2.rice.edu> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1078_1746166228.1711677642683" ------=_Part_1078_1746166228.1711677642683 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
A Java package is a grouping of classes similar to the notion of a direc=
tory is a grouping of files. Packages are used to help avoid name clashes a=
nd 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 cla=
ss part of a particular package, say funList
, you must add th=
e declaration package funList
; to the very top of the class s=
ource file.
Also, you will need to put the file in the directory structure that mirr=
ors the package name. For example, the java classes that belong to the pack=
age funList
should be in a directory also named funList. If yo=
u 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 J= ava 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 =3D 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 Ja= va. */ public ConsIntList(int f, IntList r) { first =3D f; rest =3D 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
; to the top of You need to create a subdirectory called funList
and move <=
code>IntList.java into it. The full class name for IntLi=
st
is now funlist.IntList
.
Reopen the file in the funList
subdirectory. Now compile ag=
ain. You should get error messages saying it can't find class I=
ntListVisitor
and class ConsIntList
this time. Yo=
u will need to package all the other classes/intefaces and move them into a=
ppropriate subdirectories.
funList
; declaration to the top of ConstIntList.java
, and In=
tListVisitor.java
, and move them into the funList
subd=
irectory. You should be able to compile each file individually. Try it.Note: if you use the command window to compile with the command javac, y= ou should always compile from your project's main directory. If you compile= from within a package subdirectory, it doesn't find all the supporting def= initions.
We can't run anything yet, because that's just a piece of the whole prog= ram.
TestEmptyIntList
. Do =
not make TestEmptyIntList.java
part of the package. Test=
EmptyIntList.java
does not have a package name, and is thus said to =
be in the no-name (or default) package. Save TestEmptyList.java=
in lab10 subdirectory (right above funlist
).Add code to test the accept
method of EmptyIntList . What can we do here?
If you try to compile TestEmptyIntList.java
now, you will g=
et 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=
code> class. By default, a class is "package-private", i.e., it is known wi=
thin the package, but not from outside. Try to compile again. Y=
ou should see a few error messages saying that you can't use
now by click the Test button in DrJava.EmptyInt=
List.java
because it is not public. This is because the TestEm=
ptyIntList
class is not part of the funList
package. On=
e way to resolve this problem by making TestEmptyIntList
part =
of the funList
package. A class of a package can access all th=
e 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
Please refer to the Lecture 30 notes= . Also, download and open the contained DrJava projec= t in lec29_code.zip .
Practice writing some visitors using generics:
Here's some more information on using DrJava's project capabiltiies:
DrJavaprovides a utility called Project as a way to manage large Java prog=
rams with many files and many packages. We will illustrate the use of DrJav=
aProject by writing a few visitors for the funlist
package.
In general, a project has a bunch of java files (the source code) and cl=
ass files. It is a good idea to separate the java files from the compiled c=
lass files by creating a subdirectory called bin
for the class=
files and src
for the java files.
funlist
subdirectory inside o=
f src.ListVisProj
inside of lab10
. A dialog win=
dow will popup asking for the Project Root, Build Directory and Working Dir=
ectory, etc.Set the project root directory lab10, the build directory to lab10/ src
. Click OK an=
d 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.
funlist
package to the project, use th=
e File/Open Folder and select the folder src
(and be sure the =
check the Open folder recursively checkbox). All the java files in fu=
nlist
should be displayed. Save the project. Compile the proje=
ct by clicking on the Compile Project button. Everything should compi=
le. Check the subdirectory bin to see all the class files generated b=
y the compiler.Now let's write a list visitor to compute the length of a list and add i= t to the project.
/** * Computes the length of the host list using a tail-recursive * helper visitor. */ public class GetLength implements IntListVisitor { static GetLength ONLY =3D 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. The fu=
ll class name for GetLength
and GetLengthHelp
are=
now funlist.visitor.GetLength
and funlist.visitor.GetLe=
ngthHelp
, respectively. Note that GetLength
is pu=
blic
while GetLengthHelp
is package private
. Now try to compile it! You will see a few error messages. Fix the errors=
until everything compiles.
TestGetLength
to test t=
he GetLength
visitor written in the above. Save this test clas=
s in a subdirectory of visitor called test. You will need to create a new f=
older when you try to save the test
class.=20
Be sure to save the project code periodically.
ToString
to compute a Scheme-lik=
e String representation of the list. (See ListString visitor done in lab 09=
). Package it in funlist.visitor
. Write stub code only and ma=
ke sure that everything compiles.=20
TestToString
to test <=
code>ToString . Package it in the test
subdirectory as =
done in the above step 5. Make sure it compiles.