IDI Software Component Library
© 2003 Information Disciplines, Inc., Chicago (Java pseudo class)


Conrad Weisert, Information Disciplines, Inc., Chicago


A collection of useful functions that operate on one or two integers, analogous to the standard library Math pseudo-class's functions that operate on floating-point numbers. In addition to their usefulness in application programs, some of these functions are suitable instructional examples for students in programming courses. The functions are:

OAQs (Occasionally Asked Questions)

  1. What's a "pseudo-class"?
    A packaging artifice for independent functions. One neither instantiates objects nor derives other classes from a pseudo-class. See Pseudo Classes and Quasi Classes Confuse Object-Oriented Programming.

  2. Why not document it with JavaDoc?
    Unfortunately Java's attempt to integrate usage and maintenance documentation doesn't serve either audience well. See Self-Documenting Code Unrealized from COBOL to JavaDoc.


Note: In all of the following examples, the qualifier name of the pseudo-class, IntegerFunction is omitted for readability. You should (a) insert it as a qualifier before each of the function names, (b) give the pseudo-class a shorter name and use it, or (c) move the source code for the functions you need into one of your own classes.

Convert Integer to English

public static String toEnglish(long n)

This function converts a non-negative integer to its representation in English words. For example, if k=12345678 then toEnglish(k) yields the string: "twelve million, three hundred forty-five thousand, six hundred seventy-eight", and toEnglish(0) yields "zero". Since the result string may be longer than a text box or report column, the user may have to parse it to break it into lines that fit.

Note that this function is not amenable to internationalization, since in many languages the form of a number in words is dependent on context.

Return to top of this page

Convert Integer to String

public static String toString(long n, int b)
public static String toString(long n)

This function converts an integer to its string representation in base 2 ≤ b ≤ 16. For example if k=50, then toString(k,2) yields "110010" and toString(k,16) yields "32". The single-parameter version assumes b=10.

Return to top of this page

Convert Integer to Roman numerals

public static String toRoman(int n)

If 1 ≤ n ≤ 3999, the result is the Roman numeral representation. Otherwise the Arabic numeral result is returned, i.e. the same as toString(n).

Return to top of this page

Decompose integer into prime factors

public static long[] primeFactors(long n)

The argument must be greater than 1. Upon return result[0] contains the number of prime factors,
and result[1] . . . result[result[0]] contain the factors in ascending order. For example, if you code:

         long result[] = primeFactors(60); 
then result will contain {4, 2, 2, 3, 5}, while
         long result[] = primeFactors(61); 
will yield {1, 61}. Remaining elements of the result array are undefined. (The function always allocates a 64-element array, in order to accommodate the largest possible number of factors of a 63-bit long integer).


Suggestions from Curt Sampson and Ravi Venkataraman improved the internal quality of this function.

Return to top of this page

Greatest Common Divisor and Least Common Multiple

     public static long gcd(long m, long n)
     public static long lcm(long m, long n)

Both arguments must be positive. The gcd function implements Euclid's fast-converging algorithm recursively. Overflow is possible in lcm, which uses gcd.

Return to top of this page

Integer Power of a Real Number

public static double power (double x, int n)

Java's standard Math pseudo-class contains an exponentiation function pow that takes a floating-point exponent parameter. This function, on the other hand, caters to the common case where the exponent is an integer. It may or may not execute faster than the more general function. (It is also interesting to students as an example of both recursion and functional notation.)

Return to top of this page

Trivial functions

public static long min (long m, long n)
public static long max (long m, long n)
public static long abs (long n)

These three duplicate functions found in Java's Math pseudo-class. They're included here for convenience and possible consistency.

Return to top of this page

Return to Freeware directory
Return to IDI home page

Last modified August, 2003