As CSC 153 shifts from object-oriented design and programming in Scheme to object-oriented design and programming in Java, the format of the labs will change somewhat. In particular, the main resource for the course is Experiments in Java by Samuel Rebelsky, which is available in the bookstore, and this text contains a large number of laboratory exercises. In addition, many of the labs from this text are available to on-campus users. (On-campus users may click here for a complete index.) Additional information on Java may be found in a variety of textbooks.
As you will see, each session of Experiments in Java contains a discussion, several experiments, and some post-laboratory problems. For most classes, you should read the relevant discussion materials ahead of time, so you will be appropriately prepared for the class experiments. As with our work in Scheme, I expect to comment on some parts of the material at the start of the class. However, as Experiments in Java contains many comments and notes, you should expect that in-class presentations will be somewhat shorter for this part of the course.
Most in-class activity will reference Experiments in Java with relatively few additional notes. In particular, a typical lab page will include only the following:
Lab from Experiments in Java: Session J1: An Introduction to Java
Before using Java, you must tell the Unix environment where to look for various Java libraries. This may be done in either of two ways:
Each time you open your dtterm window, type
set path = (/usr/local/java/bin $path)before you type any Java commands.
(Preferred alternative) Start the XEmacs editor, and open file .cshrc in your home directory. Then add the following lines before the final endif statement:
# set path for java set path = (/usr/local/java/bin $path)After you save the revised .cshrc file, you should open a new dtterm window, or you may type source .cshrc in an existing window before proceeding with the following steps. (The next time you log in, the machine will automatically read the .cshrc file, and this step will be done without further intervention on your part.)
If you follow this approach, you might also add the following line to help with copying files in the future.
#set alias to simplify the copying of 153 files alias rebcp "cp ~rebelsky/public_html/ExptInJava/Code/\!^.java ."
You may want to create a separate java directory, so you will have
a common place to put your Java programs. While this is not strictly
necessary, it can be a helpful organization device. To do this, open a
dtterm window, and type
To copy files to your new java, first move to that directory with the "change directory" command cd:
In most cases, the start of a lab will identify some library files that you will need, and these should be copied to your directory. For example, lab session J1 identifies the files BasicComputations.java , Greetings.java , HelloWorld.java , SimpleInput.java , SimpleOutput.java .
Use the cp command to copy these files from one place to another. Programs from Experiments in Java are in directory ~rebelsky/public_html/ExptInJava/Code/ . Thus, to copy program HelloWorld.java for this lab, you would type:
cp ~rebelsky/public_html/ExptInJava/Code/BasicComputations.java . cp ~rebelsky/public_html/ExptInJava/Code/Greetings.java . cp ~rebelsky/public_html/ExptInJava/Code/HelloWorld.java . cp ~rebelsky/public_html/ExptInJava/Code/SimpleInput.java . cp ~rebelsky/public_html/ExptInJava/Code/SimpleOutput.java .In this command, the final dot (.) represents copying the file to the current directory using the same name as before (e.g., HelloWorld.java).
Alternatively, if you added the rebcp command to your .cshrc file as suggested above, then a rebcp command has been created which does much of the above for you -- you just need to add the file name (without the .java ending). In this case, you could copy the above files with the following sequence:
rebcp BasicComputations rebcp Greetings rebcp HelloWorld rebcp SimpleInput rebcp SimpleOutputHere, you might think of rebcp as "rebelsky copy".
You may edit these .java files with XEmacs, in the same way you edit Scheme programs, labs, or other documents.
Compile a Java program with the javac command:
javac HelloWorld.javaThis creates a new, compiled program with the same name as the original program, except that the suffix .java is omitted. Normally, you will compile library files before files that use them. (The import statement in a program indicates which files need to be compiled before that program.)
java HelloWorldNote the file name indicates the compiled program you created in the previous step.
This document is available on the World Wide Web as
created April 9, 2000 by Henry M. Walker