Laboratory Exercises For Computer Science 153

An Introduction to Java

An Introduction to Java

The Shift from Scheme to Java for CSC 153 Labs

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.

New Lab Format:

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:

Getting Started With Java

Additional Notes

Read the full discussion for lab J1 first. The following steps then will help you copy, edit, compile, and run your programs.
  1. 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:

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

    mkdir java

  3. To copy files to your new java, first move to that directory with the "change directory" command cd:

    cd java

  4. 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 , , , , .

    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 for this lab, you would type:

    cp ~rebelsky/public_html/ExptInJava/Code/ .
    cp ~rebelsky/public_html/ExptInJava/Code/ .
    cp ~rebelsky/public_html/ExptInJava/Code/ .
    cp ~rebelsky/public_html/ExptInJava/Code/ .
    cp ~rebelsky/public_html/ExptInJava/Code/ .
    In this command, the final dot (.) represents copying the file to the current directory using the same name as before (e.g.,

    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 SimpleOutput
    Here, you might think of rebcp as "rebelsky copy".

  5. You may edit these .java files with XEmacs, in the same way you edit Scheme programs, labs, or other documents.

  6. Compile a Java program with the javac command:

    This 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.)

  7. Run the Java program with the java command:
    java HelloWorld
    Note 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