Class Fraction

java.lang.Object
  extended by Fraction

 class Fraction
extends Object

This class introduces fractions and enables basic operations with them.

Since:
2014-04-01
Version:
1.0
Author:
Pavel Sikora

Field Summary
(package private)  long d
          the denominator
(package private)  long n
          the numerator
 
Constructor Summary
Fraction()
          Constructs a fraction 0/1, ie. zero.
Fraction(double r)
          Constructs an estimate fraction from a real number.
Fraction(long nn)
          Constructs a fraction from an integer.
Fraction(long nn, long dd)
          Constructs a fraction from its numerator and denominator.
 
Method Summary
(package private)  Fraction abs()
          Absolute value of the fraction.
(package private)  Fraction add(Fraction a)
          Adds another fraction to this fraction.
(package private)  int compare(Fraction c)
          Compares this fraction to another fraction.
(package private)  int compare(long c)
          Compares this fraction with an integer.
(package private)  SquareRootFraction convertToSRF()
          Converts this fraction to a SquareRootFraction.
(package private)  Fraction divide(Fraction d)
          Divides this fraction by another fraction.
(package private)  boolean isEqual(Fraction c)
          Checks if this fraction is equal to another fraction.
(package private)  boolean isEqual(int c)
          Checks if this fraction is equal to an integer.
(package private)  Fraction multiply(Fraction f)
          Multiplies this fraction by another fraction.
(package private)  Fraction multiply(long f)
          Multiplies this fraction by an integer.
(package private)  Fraction squared()
          Squares this fraction.
(package private)  Fraction subtract(Fraction s)
          Subtracts another fraction from this fraction.
(package private)  String toTeX()
          Prints this fraction formatted as TeX code.
(package private)  String toText()
          Prints this fraction formatted as text.
 
Methods inherited from class
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

d

long d
the denominator


n

long n
the numerator

Constructor Detail

Fraction

Fraction()
Constructs a fraction 0/1, ie. zero.


Fraction

Fraction(double r)
Constructs an estimate fraction from a real number. The method uses convergents of continued fractions and always construct a Fraction, even if the real number is an irrational number - the resulting fraction should be correct at least to 10 decimal places. For example, for π, we'll obtain 80143857/25510582. In any case, the number we obtain should be the best approximation in the sense that there is no better approximation with an equal or lower denominator.

Parameters:
r - the real number

Fraction

Fraction(long nn)
Constructs a fraction from an integer.

Parameters:
nn - the integer (constructs fraction nn/1)

Fraction

Fraction(long nn,
         long dd)
Constructs a fraction from its numerator and denominator.

Parameters:
nn - nominator
dd - denominator
Throws:
IllegalArgumentException - Throws an exception if the denominator would be zero.
Method Detail

abs

Fraction abs()
Absolute value of the fraction.

Returns:
absolute value of this fraction

add

Fraction add(Fraction a)
Adds another fraction to this fraction.

Parameters:
a - addend
Returns:
the sum

compare

int compare(Fraction c)
Compares this fraction to another fraction.

Parameters:
c - comparand
Returns:
1 if this fraction is larger than the comparand, -1 if it is smaller than the comparand, and 0 if they are equal

compare

int compare(long c)
Compares this fraction with an integer.

Parameters:
c - comparand
Returns:
1 if this fraction is larger than the comparand, -1 if it is smaller than the comparand, and 0 if they are equal

convertToSRF

SquareRootFraction convertToSRF()
Converts this fraction to a SquareRootFraction. Basically this is just re-typing, numerical value will be preserved.

Returns:
a copy of this fraction, of type SquareRootFraction

divide

Fraction divide(Fraction d)
Divides this fraction by another fraction.

Parameters:
d - divisor
Returns:
the ratio
Throws:
ArithmeticException - Throws an exception if the nominator of the divisor is zero.

isEqual

boolean isEqual(Fraction c)
Checks if this fraction is equal to another fraction.

Parameters:
c - comparand
Returns:
true if this fraction is equal to the comparand, false otherwise

isEqual

boolean isEqual(int c)
Checks if this fraction is equal to an integer.

Parameters:
c - comparand
Returns:
true if this fraction is equal to the comparand, false otherwise

multiply

Fraction multiply(Fraction f)
Multiplies this fraction by another fraction.

Parameters:
f - factor
Returns:
the product

multiply

Fraction multiply(long f)
Multiplies this fraction by an integer.

Parameters:
f - factor
Returns:
the product

squared

Fraction squared()
Squares this fraction.

Returns:
the square

subtract

Fraction subtract(Fraction s)
Subtracts another fraction from this fraction.

Parameters:
s - subtrahend
Returns:
the difference

toTeX

String toTeX()
Prints this fraction formatted as TeX code.

Returns:
TeX code for this fraction; for example, -3/2 yields -\frac{3}{2}

toText

String toText()
Prints this fraction formatted as text.

Returns:
plain text description of this fraction; for example, -3/2 yields -3/2