Class Answer

java.lang.Object
  extended by Answer
All Implemented Interfaces:
java.lang.Comparable<Answer>

public class Answer
extends java.lang.Object
implements java.lang.Comparable<Answer>

This class presents fractions of form n/d where n and d are integer numbers. Basic operations and arithmetics for fractions are provided.

Author:
Jaanus Poial

Constructor Summary
Answer(int a, int b)
          Constructor.
 
Method Summary
 java.lang.Object clone()
          Clone of the fraction.
 int compareTo(Answer m)
          Comparision of fractions.
 Answer divideBy(Answer m)
          Quotient of fractions.
 boolean equals(java.lang.Object m)
          Equality test.
 Answer fractionPart()
          Extract fraction part of the (improper) fraction (a proper fraction without the integer part).
 int getDenominator()
          Public method to access the denominator field.
 int getNumerator()
          Public method to access the numerator field.
 int integerPart()
          Integer part of the (improper) fraction.
 Answer inverse()
          Inverse of the fraction. n/d becomes d/n.
 Answer minus(Answer m)
          Difference of fractions.
 Answer opposite()
          Opposite of the fraction. n/d becomes -n/d.
 Answer plus(Answer m)
          Sum of fractions.
 Answer times(Answer m)
          Multiplication of fractions.
static Answer toAnswer(double f, int d)
          Double value f presented as a fraction with denominator d > 0.
 double toDouble()
          Approximate value of the fraction.
 java.lang.String toString()
          Conversion to string.
static Answer valueOf(java.lang.String s)
          Conversion from string to the fraction.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Answer

public Answer(int a,
              int b)
Constructor.

Parameters:
a - numerator
b - denominator > 0
Method Detail

getNumerator

public int getNumerator()
Public method to access the numerator field.

Returns:
numerator

getDenominator

public int getDenominator()
Public method to access the denominator field.

Returns:
denominator

toString

public java.lang.String toString()
Conversion to string.

Overrides:
toString in class java.lang.Object
Returns:
string representation of the fraction

equals

public boolean equals(java.lang.Object m)
Equality test.

Overrides:
equals in class java.lang.Object
Parameters:
m - second fraction
Returns:
true if fractions this and m are equal

plus

public Answer plus(Answer m)
Sum of fractions.

Parameters:
m - second addend
Returns:
this+m

times

public Answer times(Answer m)
Multiplication of fractions.

Parameters:
m - second factor
Returns:
this*m

inverse

public Answer inverse()
Inverse of the fraction. n/d becomes d/n.

Returns:
inverse of this fraction: 1/this

opposite

public Answer opposite()
Opposite of the fraction. n/d becomes -n/d.

Returns:
opposite of this fraction: -this

minus

public Answer minus(Answer m)
Difference of fractions.

Parameters:
m - subtrahend
Returns:
this-m

divideBy

public Answer divideBy(Answer m)
Quotient of fractions.

Parameters:
m - divisor
Returns:
this/m

compareTo

public int compareTo(Answer m)
Comparision of fractions.

Specified by:
compareTo in interface java.lang.Comparable<Answer>
Parameters:
m - second fraction
Returns:
-1 if this < m; 0 if this==m; 1 if this > m

clone

public java.lang.Object clone()
Clone of the fraction.

Overrides:
clone in class java.lang.Object
Returns:
new fraction equal to this

integerPart

public int integerPart()
Integer part of the (improper) fraction.

Returns:
integer part of this fraction

fractionPart

public Answer fractionPart()
Extract fraction part of the (improper) fraction (a proper fraction without the integer part).

Returns:
fraction part of this fraction

toDouble

public double toDouble()
Approximate value of the fraction.

Returns:
numeric value of this fraction

toAnswer

public static Answer toAnswer(double f,
                              int d)
Double value f presented as a fraction with denominator d > 0.

Parameters:
f - real number
d - positive denominator for the result
Returns:
f as an approximate fraction of form n/d

valueOf

public static Answer valueOf(java.lang.String s)
Conversion from string to the fraction. Accepts strings of form that is defined by the toString method.

Parameters:
s - string form (as produced by toString) of the fraction
Returns:
fraction represented by s