# 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.

den
num
sign
Fraction(int[], int[])
Fraction(long, long)
Constructs n/d.
Fraction(MPInt, MPInt)
Constructs the fraction x/y.
Fraction(String)
The string must be (sign) integer or (sign) integer/non-zero integer.
toDecimalString(int)
The decimal expansion to N digits after the decimal point.
toString()
Just the form numerator/denominator.
num
``` public int num[]
```
den
``` public int den[]
```
sign
``` public int sign
```
Fraction
``` public Fraction(String s) throws NumberFormatException
```
The string must be (sign) integer or (sign) integer/non-zero integer.

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

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

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.

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

Overrides:
toString in class Object