Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

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.

...

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.

...

  1. 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.

...

  • 6. Write a visitor called ToString to compute a Scheme-like String representation of the list. (See ListString visitor done in lab 09). Package it in funlist.visitor . Write stub code only and make sure that everything compiles.
    1. Now write a JUnit test class called TestToString to test ToString . Package it in the test subdirectory as done in the above step 5. Make sure it compiles.
    2. Incrementally write ToString and incrementally test it until all the test code pass.

Access Permissions: (Please don't edit)

  • Set ALLOWTOPICCHANGE = Main.TeachersComp211Group

...