Class Fraction

java.lang.Object
   |
   +----Fraction

public class Fraction
extends Object
[ Math 220 main page ]

This is the class for fractions - i.e. ratios of integers. A fraction is reduced at all times.

Here is a sample program. It reads the first argument on the command line as a fraction, and the second as a natural number n. It then displays the decimal expansion of the fraction up through n digits after the decimal point.

Here is another sample program. It reads the first argument on the command line as a natural number n, sums n terms of the series 1 + 1/2 + ... + 1/n, and displays it as a fraction.

Eventually this file will contain tools for fractional arithmetic. At the moment it handles only input and output.


Variable Index

 o den
 o num
 o sign

Constructor Index

 o Fraction(int[], int[])
 o Fraction(long, long)
Constructs n/d.
 o Fraction(MPInt, MPInt)
Constructs the fraction x/y.
 o Fraction(String)
The string must be (sign) integer or (sign) integer/non-zero integer.

Method Index

 o toDecimalString(int)
The decimal expansion to N digits after the decimal point.
 o toString()
Just the form numerator/denominator.

Variables

 o num
 public int num[]
 o den
 public int den[]
 o sign
 public int sign

Constructors

 o Fraction
 public Fraction(String s) throws NumberFormatException
The string must be (sign) integer or (sign) integer/non-zero integer.

 o Fraction
 public Fraction(MPInt x,
                 MPInt y)
Constructs the fraction x/y.

 o Fraction
 public Fraction(int x[],
                 int y[])
 o Fraction
 public Fraction(long n,
                 long d)
Constructs n/d.

Methods

 o toDecimalString
 public String toDecimalString(int n)
The decimal expansion to N digits after the decimal point. Not rounded. The integral part is easy. After that it repeatedly (1) subtracts off the integral part; (2) multiplies by 10 and writes the integral part.

 o toString
 public String toString()
Just the form numerator/denominator.

Overrides:
toString in class Object