Laboratory Exercises For Computer Science 153

An Introduction to Java

The Shift from Scheme to Java for CSC 153 Labs

Summary 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 details of 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 requires some patience, but each method is reasonably straightforward. Program ~walker/public_html/courses/153.sp02/course.ss provides one 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 course.ss 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 particularly, you should be able to answer the following questions.

The Course Class in Java

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

We now consider each of these major areas, considering simple output first, then describing steps to run a Java program, and finally starting an investigation of Java syntax and semantics. We accomplish the first two parts of this work in this lab; the discussion of Java syntax and semantics continues verbally in class and in the next lab.

SimpleOutput

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 SimpleOutput class, written by Samuel Rebelsky.

  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
    
  2. Mr. Rebelsky's SimpleOutput class may be found at ~rebelsky/public_html/ExptInJava/Code/SimpleOutput.java. Copy this file to your new java directory.

  3. Program ~walker/public_html/courses/153.sp02/Course.java provides a direct translation of course.ss to Java. Copy this file to your java directory.

While we will look at Course.java 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

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:

To compile and run Java programs, the first step in MathLAN is to tell the computers where to find the relevant Java compiler and interpreter. This may be accomplished once and for all as follows:

  1. In your home directory, use an editor (e.g., xemacs) 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.

  2. Immediately after any existing alias statements, add the following lines:
    
    # alias for Java
      alias jcompile="/net/jdk1.2.2/bin/javac"
      alias jrun="/net/jdk1.2.2/bin/java"
    
    and 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:

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

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

  2. Note that the two commands may be combined on one line with the statement:
    
    jcompile Course.java && jrun 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.

The Course.java Program

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

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

  2. In comparing course.ss and Course.java, 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.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/153.sp02/lab-intro-java.html

created April 3, 2001 by Henry M. Walker
last revised March 28, 2002
Valid HTML 3.2!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.