NAVO Java Workshop, September 23, 1998
Simple Java Applications


  1. Finding the maximum of a set of numbers.

    Copy these files to your local disk.

    Average.java and Console.java

    Use your text editor to modify the program Average.java so that it also finds the maximum of the numbers typed in. Note that you don't have to modify or compile the class Console.java; its method readInt is used by Average.java.

    Hints:

    Compile the java application as described on the previous page and run.

  2. Standard Deviation

    Write a Java application that reads in a set of floating point numbers, using Java type double, and computes the mean and standard deviation of these numbers.

    You may fix the number of numbers, if you wish, or ask the user how many there are going to be.

    In addition to the program Average.java, which computes the mean of a set of integers, you may wish to look at Volume.java to demonstrate the method readDouble.

    As you read in numbers, you should store them into an array for later use in calculating the standard deviation.

    For convenience sake, here is a text version of the equation for standard deviation. Assume that there are N numbers from x1 to xN and that the mean is written xbar.

     stdev = sqrt( 1/N * (Sum from i=1 to N ((xbar - xi) ** 2))) 

    Hints:

  3. Creating and using a class.

    Create a class to generate a sequence of random numbers using the linear congruential method (as discussed in Knuth vol.2). The sequence is started by a value called the seed. Given the current value of the sequence Xn, the next value Xn+1 = ((a * Xn) + c) mod m. This will generate a sequence of numbers between 0 and m, and given good choices of a, c, and m, the sequence should be pseudo-random. In Numerical Recipes in C, the following values are recommended:

    Here is a template program for the Random Number Generator, called RNG.java. It is a class definition that has one instance variable to hold the current value, a constructor to get the seed as the initial value, and a method rand that you should change to generate random numbers. There is also a test program called RNGtest.java to generate two streams of numbers and print them.

    Part 1: Change the rand method.
    Hint: Use % for the mod operator.

    Part 2: Change the instance variables and constructor so that you can also initialize the values of a, m, and c. Test different streams in the RNGtest.java program.


Nancy McCracken at the Northeast Parallel Architectures Center.