CIT 591 Example Program: Fractions
Fall 2002, David Matuszek

Here is the complete code for the Fractions application. It's also available as a BlueJ project in this zip file.

Fractions.java
/**
 * Program to convert a decimal number into a fraction, using
 * only a few control structures.
 * 
 * @author David Matuszek
 * @version 1.2
 */
public class Fractions {
    
    public static void main(String args[]) {
    
        double decimal;
        double originalDecimal;
        int LIMIT = 12;
        int denominators[] = new int[LIMIT + 1];
        int numerator, denominator, temp;
        int MAX_GOODNESS = 100;
        
        // Get a number to be converted to a fraction
        if (args.length == 1) {
            decimal = Double.valueOf(args[0]).doubleValue();
        } else {
            // No number was given, so just use pi
            assert args.length == 0; 
            decimal = Math.PI;
        }
        originalDecimal = decimal;

        // Display the header information
        System.out.println("-------------------------------------------------------");
        System.out.println("Program by David Matuszek");
        System.out.println("Input decimal number to be converted: " + decimal);
        System.out.println();
        
        // Compute all the denominators
        System.out.println("All computed denominators:");
        int i = 0;
        while (i < LIMIT + 1) {
            denominators[i] = (int)decimal;
            System.out.print(denominators[i] + "  ");
            decimal = 1.0 / (decimal - denominators[i]);
            i = i + 1;
        }
        System.out.println();
        System.out.println();
        
        // Compute the i-th approximation
        int last = 0;
        while (last < LIMIT) {
        
            // Print out the denominators used in this computation
            System.out.print("Using these " + (last + 1) + " denominators: ");
            for (int j = 0; j <= last; j++) {
                System.out.print(denominators[j] + "  ");
            }
            System.out.println();
            
            // Initialize variables used in computation
            numerator = 1;
            denominator = 1;
            temp = 0;
            
            // Do the computation
            int current = last;
            while (current >= 0) {
                denominator = numerator;
                numerator = (numerator * denominators[current]) + temp;
                temp = denominator;
                current = current - 1;
            }
            last = last + 1;
                        
            // Display results
            double value = (double)numerator/denominator;
            int goodness = denominators[last];
            double error = 100 * Math.abs(value - originalDecimal) / originalDecimal;
            
            System.out.print("fraction = " + (int)numerator + "/" +
                             (int)denominator);
            System.out.print(", value = " + value);
            System.out.print(", goodness = " + goodness);
            System.out.println(", error = " + (int)error + "%");
            System.out.println();
                               
            // Exit early if we have reached our goodness criterion
            if (Math.abs(goodness) > MAX_GOODNESS) break;
        }        
    }
}

Example output
-------------------------------------------------------
Program by David Matuszek
Input decimal number to be converted: 3.141592653589793

All computed denominators:
3  7  15  1  292  1  1  1  2  1  3  1  14  

Using these 1 denominators: 3  
fraction = 3/1, value = 3.0, goodness = 7, error = 4%

Using these 2 denominators: 3  7  
fraction = 22/7, value = 3.142857142857143, goodness = 15, error = 0%

Using these 3 denominators: 3  7  15  
fraction = 333/106, value = 3.141509433962264, goodness = 1, error = 0%

Using these 4 denominators: 3  7  15  1  
fraction = 355/113, value = 3.1415929203539825, goodness = 292, error = 0%