CISC 370 Lecture Notes for Class No. 7, Feb 29, 2000

Course Home Prev Lect Next Lect Example Progs Exercise Solns
CoreJava Progs
Documentation
Gen'l Docs Java 2 APIs Java Glossary Term Proj Info

Graded Assignment #5 (Due Tuesday, March 7)

Reading:
Horstmann & Cornell, chap. 7 to p. 263 (for the time being we will skip the material on inner classes in chap. 6)
Exercises:

1. Add a class Diamond to the Polygon class hierarchy and then draw a stick figure with a diamond body. A diamond looks like

                  *
                 * *
                *   *
               *     *
                *   *
                 * *
                  *
      
That is, all sides are the same length and all are at a 45 degree angle (positive or negative) to the vertical (or horizontal) axis.

Do NOT change any of the existing code in the sg package. One of the principle advantages of object-oriented programming is that it is designed to re-use existing code without changing it (and hence risking the introduction of new errors).

2. (a) List three sources that you have consulted for possible term projects. Possibilities include sites on the Internet, written resources, or other persons. In each case, give a full reference to the source and a short paragraph about what kinds of information you found there. (b) List three possible topics for your term project.

Henceforth, grading will put a heavy emphasis on good object/class design using the attributes outlined on pp. 151-153 & 203-204 of the Horstmann & Cornell textbook, and as illustrated in the programming examples discussed in lecture and in the textbook. If your code merely "works" that will not be sufficient to get a passing grade.

To prepare programming assignments for submission, do the following for each exercise.

script	        // Produces a file named typescript
whoami 
date 
pwd 
cat *.java 
javac *.java     // mainFile should be the name of the file containing
java mainFile    // your main() method
exit		 // to exit script

Now print the typescript file for submittal at the beginning of lecture.

Today's Lecture Topics

  1. Homework

  2. Some term project ideas .

  3. Continuation of discussion of the simple graphics package sg .

  4. Guidelines for using your own packages. Be sure to study pp. 153-157 of the Horstmann & Cornell text first.

    Suppose you are creating a new package called chess. Do the following.

    1. Make a new directory/folder named chess.
    2. Put all the class files for the chess package in the directory/folder chess.
    3. The first non-comment line should be
           package chess;
           
    4. Add the additional directory to your CLASSPATH. The chess directory does not get added to the CLASSPATH but the directory immediately above it.

      (a) Directions for strauss.
      For example, suppose the full path for the chess directory is

                /home/base/usrc/35/13772/java/packages/chess
                
      then add the following line to the same file that has the other commands for setting your CLASSPATH (this would normally be either the file .cshrc or .localenv). Put it AFTER all the other commands that set CLASSPATH.
                setenv CLASSPATH ${CLASSPATH}:/home/base/usrc/35/13772/java/packages
                

      If you put all your packages in one place (a good idea), say in your directory named java/packages, then the above change to the CLASSPATH only has to be done once when you create the first package and not when subsequent packages are placed in the java/packages directory. However, if you put other packages in a different directory, the CLASSPATH has to be changed again using these instructions. You have to add an additional setenv for each directory where you have packages stored.

      (b) Directions for Windows.
      For example, suppose the full path for the chess directory is

                c:\java\packages\chess
                
      then use an editor to add the following line to the file c:\autoexec.bat.
                set CLASSPATH=%CLASSPATH%;c:\java\packages
                
      Put it AFTER all the other commands that set CLASSPATH. Note that chess is not on the path that is added.

      If you put all your packages in one place (a good idea), say in your directory named c:\java\packages, then the above change to the CLASSPATH only has to be done once when you create the first package and not when subsequent packages are placed in the java/packages directory. However, if you put other packages in a different directory, the CLASSPATH has to be changed again using these instructions. You have to add an additional set command for each folder where you have packages stored.

    5. Do not put files that import a package in the same folder with the package. This confuses the Java compiler.

  5. Instructions for adding to already existing packages - your packages or those of others. Note that packages can be split over different directories/folders. This is useful, for example, when you are adding to a package like sg. You can put your addition(s) in one of your own directories and yet not have to copy all the files from the $CISC370HOME/example-progs/sg directory

    Suppose you are creating a new class called Diamond that is to be added to the package sg that already has existing files in the directory $CISC370HOME/example-progs/sg. Do the following.

    1. Suppose you already have a directory
                 /home/base/usrc/35/13772/java/packages
                 
      as suggested above. Create a new subdirectory in this directory named sg. If you have not already added the
                 /home/base/usrc/35/13772/java/packages
                 
      to your CLASSPATH, do so using the instructions given above. Note that the new sg subdirectory is NOT included in the directory path added to CLASSPATH.
    2. Create your Diamond.java file in the new sg subdirectory. Be sure to put the line
                 package sg;
                 
      as the first non-comment line in the file.
    3. Compile the Diamond.java file to create Diamond.class.
    4. Put your application file, for example DiamondDemo.java, in another directory and include in it the line
                 import sg.*;
                 
    5. Now compile and execute the application in the file DiamondDemo.java.

  6. Interfaces and call back methods.

    Problem: There are many different kinds of objects that we might wish to sort. The sort methods are essentially all the same. Is there some way that we can write a good, generic sort method just once and then use it to sort many different kinds of objects?

    If we examine the code for sorting, we will see that the only difference when sorting two different kinds of objects is the method used to determine if one object precedes another in an ordering scheme that is appropriate for the given objects. So if we could write a sort method and tell it which method to use to compare objects, we would not have to keep rewriting the code each time we wish to sort a different kind of object. Suppose we assumed the compare method had a generic prototype like boolean lessThan( Object lhs,Object rhs ), then the generic sort method would always know how to invoke it. We can formalize this assumption in Java via an interface, which we do in the examples discussed below. The interface is called CallBack in the examples.

    There are two versions of the use of call backs. The first, and simpler, version passes the lessThan() method as a parameter to the generic sort method. This is illustrated by the code in the directory CISC370HOME/example-progs/callback1/ .

    The second version of the call back code is more similar to the example presented in the Horstmann and Cornell text on pp. 236-237. It uses a constructor in the Generic class to pass a handle to the particular implementation of the lessThan() method that is to be used by the generic sort method. The code for the second version is found in the directory CISC370HOME/example-progs/callback2/.

Back to the CISC 370 homepage.

This page has been accessed times since 2 Mar 2000.

Last updated 2 Mar 2000.

Corrections, suggestions and comments to Bob Caviness

Copyright 2000 B. F. Caviness