Opus5
Class Example

java.lang.Object
  |
  +--Opus5.Example

public class Example
extends java.lang.Object

Various example methods.

Version:
$Id: Example.java,v 3.4 1998/07/28 13:39:08 brpreiss Exp $
Author:
Bruno R. Preiss, P.Eng.

Constructor Summary
Example()
           
 
Method Summary
static int binarySearch(Comparable[] array, Comparable target, int i, int n)
          Returns the position of an integer in a specified contiguous region in a sorted array of integers.
static int binom(int n, int m)
          Returns the binomial coefficient, n choose m.
static void bucketSort(int[] a, int m)
          Sorts (in place) an array of integers which are known to fall in the interval [0,m-1].
static int factorial(int n)
          Returns the factorial of n.
static int fibonacci(int n)
          Returns the nth Fibonacci number.
static int fibonacci(int n, int k)
          Returns the nth Fibonacci number of order k.
static int fibonacci2(int n)
          Returns the nth Fibonacci number.
static int fibonacci3(int n)
          Returns the nth Fibonacci number.
static int findMaximum(int[] a)
          Returns the largest integer in an array of n
static double gamma()
          Approximates Euler's constant.
static int geometricSeriesSum(int x, int n)
          Computes the sum the first n terms of a geometric series in x.
static int geometricSeriesSum2(int x, int n)
          Computes the sum the first n terms of a geometric series in x.
static int geometricSeriesSum3(int x, int n)
          Computes the sum the first n terms of a geometric series in x.
static int horner(int[] a, int n, int x)
          Evaluates a polynomial in x.
static int horner2(int[] a, int n, int x)
          Evaluates a polynomial in x.
static void mergeSort(Comparable[] array, int i, int n)
          Sorts (in place) a contiguous region in array of Comparable objects.
static void one()
          Simple method to illustrate parameter passing with primitive types.
static void one2()
          Simple method to illustrate parameter passing with reference types.
static double pi(int trials)
          Computes the value of pi.
static int power(int x, int n)
          Returns x raised to the power n.
static void prefixSums(int[] a, int n)
          Computes (in place) all the prefix sums of an array of n integers.
static int sum(int n)
          Returns the sum of the first n integers.
static void two(int x)
          Simple method to illustrate parameter passing with primitive types.
static void two2(Opus5.Example.Obj x)
          Simple method to illustrate parameter passing with reference types.
static void typeset(int[] l, int D, int s)
          Finds the optimum way to typeset a justified paragraph.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Example

public Example()
Method Detail

sum

public static int sum(int n)
Returns the sum of the first n integers.
Parameters:
n - The number of integers to sum.
Returns:
The sum of the first n integers.

horner

public static int horner(int[] a,
                         int n,
                         int x)
Evaluates a polynomial in x.
Parameters:
a - The coefficients of the polynomial.
n - The degree of the polynomial.
x - The value of x.
Returns:
The value of the polynomial in x.

factorial

public static int factorial(int n)
Returns the factorial of n.
Parameters:
n - The value of n.
Returns:
The factorial of n.

findMaximum

public static int findMaximum(int[] a)
Returns the largest integer in an array of n
Parameters:
a - The array of integers.
Returns:
The largest integer in the array.

gamma

public static double gamma()
Approximates Euler's constant.
Returns:
An approximation for Euler's constant.

geometricSeriesSum

public static int geometricSeriesSum(int x,
                                     int n)
Computes the sum the first n terms of a geometric series in x. Uses an order n squared algorithm.
Parameters:
x - The value of x.
n - The number of terms to be added.
Returns:
The sum the first n terms of a geometric series in x.

geometricSeriesSum2

public static int geometricSeriesSum2(int x,
                                      int n)
Computes the sum the first n terms of a geometric series in x. Uses Horner's rule.
Parameters:
x - The value of x.
n - The number of terms to be added.
Returns:
The sum the first n terms of a geometric series in x.

power

public static int power(int x,
                        int n)
Returns x raised to the power n.
Parameters:
x - The value of x.
n - The value of n.
Returns:
x raised to the power n.

geometricSeriesSum3

public static int geometricSeriesSum3(int x,
                                      int n)
Computes the sum the first n terms of a geometric series in x. Uses the closed-form formula for the summation.
Parameters:
x - The value of x.
n - The number of terms to be added.
Returns:
The sum the first n terms of a geometric series in x.

horner2

public static int horner2(int[] a,
                          int n,
                          int x)
Evaluates a polynomial in x.
Parameters:
a - The coefficients of the polynomial.
n - The degree of the polynomial.
x - The value of x.
Returns:
The value of the polynomial in x.

prefixSums

public static void prefixSums(int[] a,
                              int n)
Computes (in place) all the prefix sums of an array of n integers.
Parameters:
a - The array of integers.
n - The length of the array of integers.

fibonacci

public static int fibonacci(int n)
Returns the nth Fibonacci number. Uses an iterative (non-recursive) algorithm.
Parameters:
n - The value of n.
Returns:
the nth Fibonacci number.

fibonacci2

public static int fibonacci2(int n)
Returns the nth Fibonacci number. Uses a recursive algorithm.
Parameters:
n - The value of n.
Returns:
the nth Fibonacci number.

bucketSort

public static void bucketSort(int[] a,
                              int m)
Sorts (in place) an array of integers which are known to fall in the interval [0,m-1]. Uses a bucket sort.
Parameters:
a - The array of integers to sort.
n - The length array of integers to sort.
See Also:
BucketSorter

binarySearch

public static int binarySearch(Comparable[] array,
                               Comparable target,
                               int i,
                               int n)
Returns the position of an integer in a specified contiguous region in a sorted array of integers. Uses a recursive, divide-and-conquer algorithm.
Parameters:
array - The array of integers.
target - The target of the search.
i - The left end of the region to be searched.
n - The length of the region to be searched.
Returns:
The position of the target in a sorted array of integers.
Throws:
java.lang.IllegalArgumentException - If the target is not in the array.

fibonacci3

public static int fibonacci3(int n)
Returns the nth Fibonacci number. Uses an efficient recursive algorithm.
Parameters:
n - The value of n.
Returns:
the nth Fibonacci number.

mergeSort

public static void mergeSort(Comparable[] array,
                             int i,
                             int n)
Sorts (in place) a contiguous region in array of Comparable objects.
Parameters:
array - The array of comparable objects to be sorted.
i - The position of the left-most element to be sorted.
n - The length of the region to be sorted.
See Also:
TwoWayMergeSorter

fibonacci

public static int fibonacci(int n,
                            int k)
Returns the nth Fibonacci number of order k. Uses a dynamic-programming algorithm.
Parameters:
n - The value of n.
k - The value of k.
Returns:
the nth Fibonacci number of order k.

binom

public static int binom(int n,
                        int m)
Returns the binomial coefficient, n choose m. Uses a dynamic-programming algorithm.
Parameters:
n - The value of n.
m - The value of m.
Returns:
The binomial coefficient, n choose m.

typeset

public static void typeset(int[] l,
                           int D,
                           int s)
Finds the optimum way to typeset a justified paragraph.
Parameters:
l - The lengths of the words in the paragraph.
D - The length of a line.
s - The length of the normal interword space.

pi

public static double pi(int trials)
Computes the value of pi. Uses a monte-carlo algorithm.
Parameters:
trials - The number of trials to be done.
Returns:
The value of pi.

one

public static void one()
Simple method to illustrate parameter passing with primitive types.

two

public static void two(int x)
Simple method to illustrate parameter passing with primitive types.

one2

public static void one2()
Simple method to illustrate parameter passing with reference types.

two2

public static void two2(Opus5.Example.Obj x)
Simple method to illustrate parameter passing with reference types.