CSC 153 Grinnell College Spring, 2005
Computer Science Fundamentals
Laboratory Exercise

An Introduction to Java

The Shift from Scheme to Java for CSC 153 Labs


This laboratory begins a transition from working with the Scheme programming language to Java. The lab first reviews a simple object-oriented program and test suite written in Scheme and then considers the direct translation of the program and testing to Java. The lab then considers two approaches for compiling and running the Java program.

A Simple Course Class in Scheme

Consider a simple class that would maintain information about a course (e.g., CSC 153). Each object in the class might have the following characteristics:

Writing this course class in Scheme requires some patience, but each method is reasonably straightforward. Program ~walker/public_html/courses/153.sp05/scheme/ provides one Scheme implementation of such a class.

  1. Copy this program to your account, and load it into Scheme.

Testing of the course class might involve forming two objects (using the two construction options), retrieving the fields of the each object, modifying some fields, and printing the fields again.

  1. Test with the following sequence of commands:
    (define myCourse (course "CSC"  153  4  "CS Fundamentals"))
    (define yourCourse (course "Math"))
    ;; test of initialization, toString, and extractor methods
    (myCourse 'toString)
    (yourCourse 'getSubject)
    (yourCourse 'getNumber)
    (yourCourse 'getTitle)
    (yourCourse 'getCredits)
    ;; set fields
    (yourCourse 'setNumTitle 131 "Calculus I")
    (yourCourse 'setCredits 4)
    ;; print results of modification
    (yourCourse 'toString)

  2. Be sure you understand how this code works. In particular, you should be able to answer the following questions.

Moving from Scheme to Java

The initial move from Scheme to Java requires at least four major adjustments:

In this lab, we address the last three of these major areas, considering the organization of classes into packages first, then reviewing a simple alternative for output, next describing steps to run a Java program, and finally starting an investigation of Java syntax and semantics. We accomplish the last three parts of this work in this lab; the discussion of Java syntax and semantics continues verbally in class and in the next lab.


As we start our exploration, pragmatics require us to decide how we will manage our work. There are two basic choices:

Each of these approaches has its merits:

Since each of these approaches has its merits, the rest of this lab discusses the details of each in parallel.

As you will see, many ideas of Java and programming are common to both approaches. However, the mechanics are different. One approach may seem simpler for some steps, but the other approach may seem simpler for other steps.

You are encouraged to experiment with both of these approaches.

The Course Class in Java

We first consider a conceptual view of file management for Java programs.


Java groups related classes together into units, called packages. Within a Unix (or Linux) system, each package must have its own directory. Each class related to that package then is stored in a separate file within the package directory.

Directory Hierarchies

To illustrate a typical directory structure, consider the program examples and programs we will write for this course. The following discussion uses different directory names for our two approaches, so we can keep them straight.

Programmer Management for Java Programming Management Working

The instructor's base directory for Java examples is ~walker/java/examples. That is, within my home directory ~walker, I have a subdirectory java where I put most or all of my Java programs. Within this java, programs are organized according to their uses. Sample programs for this course are located within an examples subdirectory.

The instructor's base directory for eclipse examples is ~walker/java-code/eclipse/examples. That is, my home directory ~walker has a subdirectory java-code, and that in turn has a subdirectory eclipse. Eclipse further organizes work into projects, and examples is a sample project subdirectory. Unless otherwise specified, the eclipse integrated environment expects all packages and programs to be within a project subdirectory.

The first example, class Course, is defined in a file, called, and this file is part of a package course. The names seem similar, although capitalization matters in both Unix/Linux and Java. The conventions are:
  • use a lower-case first letter for package names;
  • use an upper-case first letter for class names;
  • use the class name as the file name (with a suffix .java);
  • when a package or class name consists of several words, put the words together, capitalizing the first letter of subsequent words (e.g., mathComputations).

Programmer Management for Java Programming Management Working

The initial directories and files for my copy of program are organized into the following hierarchy:
my directory and file hierarchy for

The packages and files managed by the eclipse integrated environment are subdirectories of an eclipse environment.
my directory and file hierarchy for

Programmer Management for Java Programming Management Working

The first part of this lab asks you to construct a similar framework for your Java programs within your account, except that you likely will want to omit the "examples" directory. That is, subdirectory course will come directly under your java directory.

When using the Programmer Management approach, you construct this framework explicitly by issuing commands in a terminal window:

  1. To organize your forthcoming work in Java, open a terminal window, and create a new directory with the command
    mkdir java
    Now move to that directory with the command
    cd java
    Create new course and mathComputations subdirectories with the command
    mkdir course
    mkdir mathComputations
    Move to the course directory
    cd course

When using Eclipse, directory management is handled by the eclipse integrated environment. Since the setup of this system requires some tedium, we usually let a Unix script (program) do this work for us. For this course, I suggest you use the existing script /home/walker/153/java/eclipse. Since this is an awkward command to remember and type, we create an alias:
  1. In your home directory, use an editor (e.g., emacs) to open the file .bashrc.

    This file is read whenever a process (e.g., a window) is started on these computers. Within this file, you probably will see several alias statements. These statements give easily used abbreviations for common commands, to make our work simpler.

    Immediately after any existing alias statements, add the following lines:

        # set variable for Eclipse Java IDE environment
          alias eclipse="/home/walker/153/java/eclipse &"

    The command eclipse now will be defined for any new windows that are opened. (If you want to use this command for windows already open, type the command

       source ~/.bashrc

    at the prompt in the relevant window.)

    With this alias defined, when you type eclipse in a terminal window, the correct directory structure will be set up, and the eclipse environment will be started. Further, when you create packages and classes, eclipse will place them correctly within the subdirectory hierarchy; details are given in the next step.

  1. Program ~walker/java/examples/course/ provides a direct translation of to Java. Copy this file to your course subdirectory.

To get started within the eclipse integrated environment, we need to create a new project for our work, a new package within the project, and a new class within the package.

These steps are all accomplished with the File menu within the eclipse environment.

  1. Within a terminal window, type eclipse to start this environment (be prepared to wait awhile for the environment to start up). Then follow these steps within the eclipse environment :

    1. Create a New Project: Select New from the File menu, then select Project, then select Java Project, and then name the project (e.g., examples).
    2. Create a New Package: Select New from the File menu, then select Package, and then name the package (e.g., course).
    3. Create a New Class: Select New from the File menu, then select Class, and then name the class (e.g., Course).

    Once the new class Course is registered with eclipse, we need to add the proper code. One simple way to do this is to delete the class stub created by eclipse. Then bring up program ~walker/java/examples/course/ in a browser, highlight the full program, and copy-and-paste it into the code window within eclipse. Use the File menu in eclipse to save the file.

The PrintWriter Class

Within Java, all work is done through the use of relevant classes and objects. In particular, an output class must be utilized to print material to the screen. In many applications, rather than write such a class ourselves, we utilize a class written by someone else. In this case, we will use a standard Java class PrintWriter.

Class PrintWriter has several useful methods for writing to the screen, including print to send material to the output device, and println to send material, with a newline character at the end.

The use of PrintWriter involves three main steps:

  1. import statement to tell Java that the program will reference this predefined Java class,
  2. Create an object (out in this example) of class PrintWriter, so there is an instance of this class available to do the actual work, and
  3. Use methods from out to perform the actual output; in this case print sends material to the output device, and println sends material, with a newline character at the end.

After the initial import and creation, the print and println methods may be used repeatedly as desired, without additional initialization.

  1. Examine program to find how this program implements each of these steps.

While we will look at shortly, we first consider how to run a Java program.

Compiling and Interpreting

As you may know, computers work with a number of different "languages". Each computer really understands only one language: its underlying machine language. We make computers understand another language by either

  • writing programs that translate the other language to the computer's machine language (compilation)
  • writing programs that read programs written in the other language and then directly executing those programs (interpretation)

Already in this course, you have run Scheme programs, and these run using the second approach -- with a Scheme interpreter.

Java, however, uses an interesting combination of compilation and interpretation:

  • You must first compile your Java program to Java Virtual Machine (JVM) code.
  • You then interpret the JVM code.

Programmer Management for Java Programming Management Working

To compile and run Java programs directly, the first step is to tell the computers where to find the relevant Java compiler and interpreter. A second step is to tell the Java interpreter where to find your class libraries (it already knows about the pre-defined classes, but you need to include your own materials by setting a CLASSPATH variable in your account). This may be accomplished once and for all as follows:
  1. In your home directory, use an editor (e.g., emacs) to open the file .bashrc.

    This file is read whenever a process (e.g., a window) is started on these computers. Within this file, you probably will see several alias statements. These statements give easily used abbreviations for common commands, to make our work simpler.

    Immediately after any existing alias statements, add the following lines:

    # aliases for Java
      alias jcompile="/opt/jdk1.5.0/bin/javac"
      alias jrun="/opt/jdk1.5.0/bin/java"
    # set variable for Java class libraries
    export CLASSPATH="$CLASSPATH:/home/~yourUsername/java:.:/home/walker/java/examples"

    where you fill in yourUsername for MathLAN in the place shown. After editing, save your file.

    The commands jcompile and jrun now will be defined for any new windows that are opened. (If you want to use these commands for windows already open, type the command

       source ~/.bashrc

    at the prompt in the relevant window.)

    Execution of Java programs now involves two steps:

    • To compile Java programs, write
      % jcompile
      That is, compile the program called, creating a compiled program called file.class (with the Java compiler)

    • To interpret compiled Java programs, write
      % jrun packageName.file

      That is, run the compiled program called file.class within the packageName package (with the Java interpreter).

    We now use these commands to run the above test cases for

    1. Use jcompile and jrun to compile and run the program

    2. Note that the two commands may be combined on one line with the statement:
      jcompile && jrun packageName.Course

      In this command, the connection && is read as a "conditional and". That is, the jcompile command is issued. Then, if the compilation proceeds without error, the jrun command is followed. However, if jcompile produces an error, then jrun is ignored.

  1. The eclipse integrated environment handles compiling and running of programs automatically. To run a program, follow these steps:

    1. Use the Package Explorer on the left side of the environment to highlight the program If this is not immediately visible, use the small triangles on the left to show parts of the relevant project and package:
      1. Packages within a project can be displayed by clicking on the small triangle to the left of the project name, so that the triangle points downward.
      2. Classes within a package can be displayed similarly by clicking on the triangle to the left of the package.
    2. With the Class code highlighted in the left "Package Explorer" panel: Select Run As from the Run menu, and then select Java Application.

      • If the code is syntactically correct, the program's output in the window at the bottom of the screen.

      • If the code is not syntactically correct, a red "X" will appear to the left of the offending line in the Class window above.

The Program

Java programs may be edited with Emacs or eclipse, in the same way you edit Scheme programs, labs, or other documents.

  1. Bring into your editor, so you can view it carefully.
    Also, print out a copy of

  2. In comparing and, all field names and method names are exactly the same, and the methods do the same work in the same way. Try matching up elements of the two programs just by looking at the listings.

Either during the last part of today's class or in the next class, the class will talk about the parallels of the two courses, and the specific syntax and semantics of Java will be discussed at some length.

  Management Working
  1. When you are done working with eclipse, be sure to exit using the File menu. Eclipse maintains may internal records, and these may lose synchronization if you close by clicking the "X" in the upper right of the window rather than using the File menu.

    Always quit eclipse using the File menu!

This document is available on the World Wide Web as

created 3 April 2001
last revised 3 April 2006
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at