/* * 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 RecursiveContinuedFraction { /* * 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. It does so by starting * the recursive function evalContFracRecur at position 0. */ public static double evaluateContinuedFraction(int[] fracArray) { return evalContFracRecur(fracArray, 0); } /* * The function evalContFracRecur recursively evaluates a continued * fraction. It does so by adding the current element in the array to * one divided by the result of evaluating the rest of the array. * It takes as parameters the integer array to be evaluated, and an * int indicating the current position in the array to control the * end of the recursion when it reaches the end of the array. */ public static double evalContFracRecur(int[] fracArray, int pos) { int last = fracArray.length - 1; // Index of last number in fracArray. /* * If we are at end of array, we return value of last element. */ if (pos == last) { return (double)fracArray[last]; } /* * Otherwise, we add the current element plus the quantity of * one divided by the result of evaluating the rest of the array. * The rest of the array is evaluated recursively with the position * parameter advanced forward by one. */ else { return plusInverse(fracArray[pos], evalContFracRecur(fracArray, pos+1)); } } /* * 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). The function works by * starting the recursive function getSimpFracRecur at position 0. */ public static int[] getSimpleFraction(int[] fracArray) { return getSimpFracRecur(fracArray, 0); } /* * The function getSimpFracRecur recursively computes the simple * fraction for an array representing a continued fraction. It * does so by combining the current element in the array with * the simple fraction for the rest of the array. * It takes as parameters the integer array to be converted, and an * int indicating the current position in the array to control the * end of the recursion when it reaches the end of the array. */ public static int[] getSimpFracRecur(int[] fracArray, int pos) { int last = fracArray.length - 1; // Index of last number in fracArray. /* * 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]; /* * If we are at end of array, we return the fraction consisting * of the last array element over 1. */ if (pos == last) { simpleFraction[0] = fracArray[last]; simpleFraction[1] = 1; } /* * Otherwise, we combine the current element with the simple fraction * for the rest of the array. To combine them, we invert the simple * fraction for the rest of the array, and get a common denominator to * add it to the current element. The simple fraction for the rest * of the array is computed recursively with the position parameter * advanced forward by one. */ else { int[] restOfFrac = getSimpFracRecur(fracArray, pos + 1); int newDenom = restOfFrac[0]; int newNum = fracArray[pos]*newDenom + restOfFrac[1]; simpleFraction[0] = newNum; simpleFraction[1] = newDenom; } return simpleFraction; } }