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.
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.sp01/course.ss provides one implementation of such a class.
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.
(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)
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.
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.
mkdir javaNow move to that directory with the command
cd java
While we will look at Course.java shortly, we first consider how to run a Java program.
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:
.bashrc.
alias statements. These statements give easily used
abbreviations for common commands, to make our work simpler.
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:
% jcompile file.java
javac (Java compiler).
% jrun file
java (Java interpreter).
We now use these commands to run the above test cases for Course.java.
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.
Java programs may be edited with XEmacs, in the same way you edit Scheme programs, labs, or other documents.
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.math.grin.edu/~walker/courses/153.sp00/lab-intro-java.html
created April 3, 2001 by Henry M. Walker