/* * A class to process continued fractions represented as a list of * integers. The main function takes the list as command-line arguments * and outputs the floating-point value of the continued fraction and * the numerator and denominator of the corresponding simple fraction. */ public class ContinuedFraction { /* * The main function calls functions to make the command line arguments * into an int array, compute the value of the continued fraction, and * compute the simple fraction. Then it prints out these results. */ public static void main(String args[]) { int[] argsAsInts = stringArrayToIntArray(args); double contFracValue = evaluateContinuedFraction(argsAsInts); System.out.println("The value of the continued fraction is " + contFracValue); int[] simpleFraction = getSimpleFraction(argsAsInts); System.out.println("The simple fraction is " + simpleFraction[0] + "/" + simpleFraction[1]); } /* * The function stringArrayToIntArray takes as input a String array * which is assumed to contain Strings that represent ints. It returns * an int array containing the ints represented by each String from * the String array. */ public static int[] stringArrayToIntArray(String[] s) { int[] intArray = new int[s.length]; // Allocate the int array. for(int i = 0; i < s.length; i++) // Loop for each element in String array. { intArray[i] = Integer.parseInt(s[i]); //Parse element in String array. } return intArray; } /* * The function evaluateContinuedFraction takes as input an int array * and returns a double which is the floating point value of the * continued fraction represented by the array. */ public static double evaluateContinuedFraction(int[] fracArray) { int len = fracArray.length; // Length of the array, to control loop. /* * In the following loop, we start at the end of the array, and * repeatedly add 1 divided by result so far with the preceding * element in the array until we reach the start of the array. */ double result = (double)fracArray[len-1]; for (int i = len - 2; i >= 0; i--) { result = plusInverse(fracArray[i], result); } return result; } /* * The function plusInverse takes as input an int a and double b * and returns a + 1/b. */ public static double plusInverse(int a, double b) { return a + 1/b; } /* * The extra credit conversion to a simple fraction. The function * getSimpleFraction takes as input an int array and returns an * int array containing the numerator and denominator of the * simple fraction represented by the continued fraction. Note * that we don't reduce the fraction to lowest terms (that was * beyond the scope of the assignment). */ public static int[] getSimpleFraction(int[] fracArray) { int len = fracArray.length; // Length of the array, to control loop. /* * We maintain an array of two ints whose first element will * contain the numerator and second element will contain the * denominator of the simple fraction. */ int[] simpleFraction = new int[2]; /* * In the following loop, we start at the end of the array. We * repeatedly combine the preceding element in the array with * the inverse of the simple fraction found so far by getting * the common denominator and adding them, until we reach the * start of the array. */ simpleFraction[0] = fracArray[len-1]; simpleFraction[1] = 1; for (int i = len - 2; i >= 0; i--) { int newDenom = simpleFraction[0]; int newNum = fracArray[i]*newDenom + simpleFraction[1]; simpleFraction[0] = newNum; simpleFraction[1] = newDenom; } return simpleFraction; } }