/* * Rational.java 2005-05-13 * * Dirk Ansorge, FernUniversitaet Hagen * */ package twodsack.util.number; /** * The Rational class is an abstract class that defines a set of methods. * All those methods must be implemented by a class that implements * a Rational type for the 2DSACK package. How a Rational type is implemented is left to the implementor. He may implement is as a pair * of integers, as double or whatever. However, some of the methods are especially for Rational types (such as {@link #getDenominator()}) and * they must be implemented.

* For equality checks, there are two mechanisms in the 2DSACK package. For a class implementing the Rational type, a boolean field named PRECISE * tells whether a number of type Rational (named deviation) or two numbers of type double (named DEVIATION_DOUBLE and DEVIATION_DOUBLE_NEG) * are used. Make sure, that those fields are implemented. * * Furthermore, there must be constructors for the following types:

*

*/ abstract public class Rational { /* * constructors */ /** * The standard constructor. */ public Rational(){} /** * Returns the numerator of the Rational. * * @return the numerator as int */ abstract public int getNumerator(); /** * Returns the denominator of the Rational. * * @return the denominator as int */ abstract public int getDenominator(); /** * Sets this to r. * * @param r the new Rational value r */ abstract public void assign(Rational r); /** * Sets this to i. * * @param i the new Rational value i */ abstract public void assign(int i); /** * Sets this to d. * * @param d the new Rational value d */ abstract public void assign(double d); /** * Returns this * r. * * @param r the second factor * @return product of this and r */ abstract public Rational times(Rational r); /** * Returns this * i. * * @param i the second factor */ abstract public Rational times(int i); /** * Returns this * r. * Stores the result in in. * * @param r the second factor * @param in the result is stored in this variable * @return this * r */ abstract public Rational times(Rational r, Rational in); /** * Returns this : r. * * @param r the divisor * @return this : r */ abstract public Rational dividedby (Rational r); /** * Returns this : i. * * @param i the divisor * @return this : i */ abstract public Rational dividedby (int i); /** * Returns this : r. * The result is stored in in. * * @param r the divisor * @param in the result is stored in this variable * @return this : r */ abstract public Rational dividedby (Rational r, Rational in); /** * Returns this + r. * * @param r the summand * @return this + r */ abstract public Rational plus(Rational r); /** * Returns this + i. * * @param i the summand * @return this + i */ abstract public Rational plus(int i); /** * Returns this + r. * The result is stored in in. * * @param r the summand * @param in the result is stored in this variable * @return this + r */ abstract public Rational plus(Rational r, Rational in); /** * Returns this - r. * * @param r the minuend * @return this - r */ abstract public Rational minus(Rational r); /** * Returns this - i. * * @param i the minuend * @return this - i */ abstract public Rational minus(int i); /** * Returns this - r. * The result is stored in the variable in. * * @param r the minuend * @param in the result is stored in this variable * @return this - r */ abstract public Rational minus(Rational r, Rational in); /** * Returns true, if this is less than r. * * @param r the Rational to compare with * @return true, if this < r */ abstract public boolean less(Rational r); /** * Returns true, if this is less than i. * * @param i the int to compare with * @return true, if this < i */ abstract public boolean less(int i); /** * Returns true, if this is equal to r. * * @param r the Rational to compare with * @return true, if this = r */ abstract public boolean equal(Rational r); /** * Returns true, if this is equal to i. * * @param i the int to compare with * @return true, if this = i */ abstract public boolean equal(int i); /** * Returns true, if this is greater than r. * * @param r the Rational to compare with * @return true, if this > r */ abstract public boolean greater(Rational r); /** * Returns true, if this is greater than i. * * @param i the int to compare with * @return true, if this > i */ abstract public boolean greater(int i); /** * Compares this and r and returns one of {0, 1, -1}.

* Returns 0, if this = r

* Returns -1, if this < r

* Returns 1 otherwise * * @param r the Rational to compare with * @return one of {0, 1, -1} as byte */ abstract public byte comp(Rational r); /** * Returns true, if this <= r. * * @param r the Rational to compare with * @return true if this <= r */ abstract public boolean lessOrEqual(Rational r); /** * Returns true, if this >= r. * * @param r the Rational to compare with * @return true if this >= r */ abstract public boolean greaterOrEqual(Rational r); /** * Returns this as int. * * @return this as int */ abstract public int getInt(); /** * Returns this as double. * * @return this as double */ abstract public double getDouble(); /** * Converts this to a String. * * @return this as String */ abstract public String toString(); /** * Returns a copy of this. * * @return the copy */ abstract public Rational copy(); /** * Returns the absolute value of this. * * @return |this| */ abstract public Rational abs(); /** * Rounds this to i digits. * * @param i the number of digits */ abstract public void round(int i); /** * Sets an field of the class to b. * The implementor can decide, whether the class should have a 'precise' and a 'less precise' implementation. By using this method * a flag can be set to use the more or less precise version. PRECISE=true means, that the deviation value is automatically set to 0. * * @param b PRECISE is set to this value */ abstract public void setPrecision(Boolean b); /** * Returns the deviation value for computations with deviation = true. * * @return the deviation value */ abstract public Rational getDeviation(); /** * Sets the deviation value deviation. * This number is used for equality checks when PRECISE = true. * * @param r the new deviation value */ abstract public void setDeviation(Rational r); /** * Sets the deviation values DEVIATION_DOUBLE and DEVIATION_DOUBLE_NEG. * This value is used for PRECISE = false. * DEVIATION_DOUBLE is set to d and DEVIATION_DOUBLE_NEG is set to -d. * * @param d the new deviation value */ abstract public void setDeviationDouble(Double d); /** * Returns the DEVIATION_DOUBLE value. * * @return the deviation value */ abstract public double getDeviationDouble(); /** * Returns the DEVIATION_DOUBLE_NEG value. * * @return the deviation value. */ abstract public double getDeviationDoubleNeg(); }//end class Rational