These examples illustrate some of the sequential features of the language. An on-line tutorial is available from Sun Microsystems for a more detailed discussion of the syntax and semantics of the Java language. A basic FAQ (Frequently Asked Questions) and a programmer's FAQ are also available.
These three examples show how to write a single-class program containing one or more static procedures or functions and a main() method where execution of the program begins.
This example shows how to access a static method defined in another class.
This example shows how to read numbers and words (strings) from the keyboard (or stdin in UNIX terminology).
This example shows how to open a file and write binary floating-point numbers (not translated into ASCII) to the file. The numbers are read from the keyboard with a tokenizer as in the previous example. Then the file is read and the numbers printed out. The file is deleted in the finally block whether or not there is an IO error.
This next example shows how to read and parse a text file containing some input data in a specific format.
This example shows how to create objects from user-defined classes.
The GetOpt class can be used to process command line arguments when a Java program is run, like the following
javac aClass.java java aClass -a -f theFile -w -80 -h3.33 arg1 arg2
This last example shows how to subclass the Bank and Account example.
A genetic algorithm is an optimization technique that uses randomization instead of a deterministic search strategy. To maximize f(x) over some domain, the values of x in the domain are encoded in strings over some alphabet, usually {0,1}. A population of such strings, called chromosomes, is created at random. Then the genetic operators selection for reproduction, crossover, and mutation are applied to the population members. Selection is based on the fitness value of the chromosome, usually f(x). After this is repeated many times, the population contains mostly highly fit chromosomes, one of which (it is hoped) is close to the maximum value of the function f(x) over the domain.
This example shows how to split a program up into packages. The Chromosome class is defined as a hierarchy of subclasses for each gene data type.
The XtangoAnimator class in the XtangoAnimation directory of the lib directory contains a collection of methods that implement the command set of the Xtango algorithm animation interpreter, animator. A Java program can create an instance of XtangoAnimator and call its methods to create graphical icons of various colors in a window and then move them around.
The XtangoAnimator class is designed to be used in a stand-alone application for algorithm animation. In contrast, the Xtango animator program reads commands from a text file or UNIX pipe and interprets them. A group at Duke University has implemented a Samba interpreter, called lambada. Samba is a superset of Xtango. The lambada interpreter also reads a text stream of commands from a file or pipe. The XtangoAnimator class defines methods that correspond to the Xtango animator commands and that can be called in a Java program.
Here are two example programs.
A sorting screen snapshot:
Last modified 30 December 1997.
© 1997 Stephen J. Hartley
SJH shartley@mcs.drexel.edu