Sequential Example Programs

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.

    Compute Factorials.

    Sort Numbers.

    N-Queens Problem.

This example shows how to access a static method defined in another class.

    Two Class Factorial Program.

This example shows how to read numbers and words (strings) from the keyboard (or stdin in UNIX terminology).

    Tokenizing Keyboard Input.

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.

    Writing and Reading a Data File.

This next example shows how to read and parse a text file containing some input data in a specific format.

    Tokenizing File Input.

    The Input File.

This example shows how to create objects from user-defined classes.

    Neighborhood Banking.

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
    

    Class GetOpt Helps Parse Command Line Options.

This last example shows how to subclass the Bank and Account example.

    Commercial Banking.

A Simple Genetic Algorithm

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.

    README file.

    sGA.java

    Debug.java

    Defaults.java

    Globals.java

    MyRandom.java

    ShellSort.java

    Chromosome.java

    BitChromosome.java

    DoubleChromosome.java

    BitCountChromosome.java

    ByteCountChromosome.java

    SinesChromosome.java

    XtoTenthChromosome.java

    MyChromosome.java

    Crossover.java

    OnePointCrossover.java

    UniformCrossover.java

    NPointCrossover.java

    SelectionException.java

    FitnessSumZeroException.java

    Selection.java

    ProportionalSelection.java

Algorithm Animation

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.

    ``Hello, world!'' using XtangoAnimator.

    Animated Bubble Sorting.

A sorting screen snapshot:

Laboratory Exercises

  1. Write a Java program that generates prime numbers. Try both alternatives for getting input data into the program: command line arguments and keyboard input. Write the program first as one class, then as several classes.
  2. Adaptive Quadrature
  3. Unidirectional TSP
  4. Animate the N-Queens program using the XtangoAnimator class.
  5. Look up the ``Game of Life'' in Scientific American and write a Java program to simulate it. Is the issue available on the Web? Animate your program.


Last modified 30 December 1997.

© 1997 Stephen J. Hartley

SJH
shartley@mcs.drexel.edu