Homework 5 : Continued Fraction
CMP12a Winter 2002
Thomas Raffill

Due on the evening of Monday, April 29 by midnight.

Note: This assignment is graded, so do your best.

Introduction

The program will input integers and print out the value of a continued fraction composed of these integers. Let the integers be a1, a2, a3, ... ak. The continued fraction is this:
continued fraction = a1 + 1/(a2 + 1/(a3 + ... + 1/ak)...)
For example, if the numbers are 1, 2, 3, then the continued fraction is 1 + 1/(2 + 1/3). The value to be printed is then a floating point approximation of this continued fraction, which is 1.4285714285714286.
Hint: You might want to use the plusInverse function from HW4 in a loop to evaluate the continued fraction.

Web Sites about Continued Fractions

An Introduction to Continued Fractions
Continued Fractions ...an Introduction

Input

A list of integers. The program takes as input any number of integers by command-line arguments. You don't need to validate the input! In other words, you can assume the program will always be run with a list of command line arguments which are positive integers.

Output

Using given integers, calculate the value of the continued fraction and print out to standard output "The value of the continued fraction is" followed by its value.

Examples

  • Input: (At the command line)
    java ContinuedFraction 1 2 3
    
  • Output: (To standard output)
    The value of the continued fraction is 1.4285714285714286
    
  • Input: (At the command line)
    java ContinuedFraction 1 2 3 4
    
  • Output: (To standard output)
    The value of the continued fraction is 1.4333333333333333
    

    Skeleton code

    This is skeleton code to help you get started on your program.
    /* Homework 5
     * ContinuedFraction.java
     *
     * Author : Sung Kim <hunkim@cse>
     */
    class ContinuedFraction {
    
    public static void main (String[] args) {
      int numberOfInputs = args.length;
      int inputsAsInts[] = new int[numberOfInputs];
      // Process command-line arguments using for loop.
      // Use a loop to calculate value of continued fraction and print results.
      }
    
      static double plusInverse(int a, double b) {
      // Same as in the solution to homework 4. 
      }
    }
    
    

    Extra Credit

    For extra credit, compute the numerator and denominator of the simple fraction equal to the given continued fraction. For example, if the continued fraction is 1 + 1/(2 + 1/3), the equivalent simple fraction is 10/7, so the numerator is 10 and the denominator is 7. Print out the sentence, "The simple fraction is a/b" where a is the numerator and b is the denominator of the simple fraction.

    Examples

  • Input: (At the command line)
    java ContinuedFraction 1 2 3
    
  • Output: (To standard output)
    The value of the continued fraction is 1.4285714285714286
    The simple fraction is 10/7 
    
  • Input: (At the command line)
    java ContinuedFraction  1 2 3 4
    
  • Output: (To standard output)
    The value of the continued fraction is 1.4333333333333333
    The simple fraction is 43/30
    

    Notes

  • Start early. Read the newsgroup.
  • Do your own work. Don't submit other people's code.
  • Use arrays.
  • You don't need to validate input values.
  • The extra credit is harder! Make sure you have a working solution to the basic problem before you attempt the extra credit problem! Also, if you have a working version, make sure you save a backup file of it before you modify it!

    To Submit Homework

    You only need to submit your source code. DON'T submit other files except your source code. When submitting homework, use the following form (when logged on to your cats account):
         submit cmps012a-tr.s02 hw5 ContinuedFraction.java
    
         submit is the script on cats you use to turn in your
         homework.
    
         cmps012a-tr.s02 is the class locker for this class. 
    
         hw5 is for homework 5. This designation will be hwn where n is the
         number of the homework assignment.
    
         ContinuedFraction.java is the source file name.
    

    To Check Homework

         peek cmps012a-tr.s02 hw5
    

    Questions ?

  • Use the newsgroup ucsc.class.cmps012a.
  • Attend labs and office hours.
  • Send an email to the instructor or TA.