
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 |
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.
Suppose you are creating a new package called chess. Do the following.
package chess;
(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.
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.
/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.
package sg;
as the first non-comment line in the file.
import sg.*;
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/.
Generic( CallBack c ){
cb = c; // cb is an instance of the class that is
} // implementing the lessThan() method used
// by sort()
Also note that sort() is now a member method (i.e., it is no
longer static).  cb is the handle by which sort()
gets access to the desired implementation of lessThan().
 sort() no longer needs the parameter for the
lessThan() method which makes its params a bit more natural.
Generic generic =
new Generic(new GenericSortTester1());
This code sends a handle to the method lessThan() for use in the
Generic class. It is sent via an instance of the class
GenericSortTester1 that implements the needed version of
lessThan(). This code also creates the object
generic that is needed to invoke the member method
sort()
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