Skip to content

Instantly share code, notes, and snippets.

@ahmednasserpro
Created May 14, 2019 03:15
Show Gist options
  • Save ahmednasserpro/ef72ac9ab898a7b049ad95320b5e1bf5 to your computer and use it in GitHub Desktop.
Save ahmednasserpro/ef72ac9ab898a7b049ad95320b5e1bf5 to your computer and use it in GitHub Desktop.
The Rational class is used for rational number. A rational number consists of a numerator and a denominator. There are many equivalent rational numbers—for example, 1/3 = 2/6 = 3/9 = 4/12. The numerator and the denominator of 1/3 have no common divisor except 1
/**
* The {@link Rational} class is used for rational number.
* A rational number consists of a numerator and a denominator.
* There are many equivalent rational numbers—for example,
* 1/3 = 2/6 = 3/9 = 4/12.
* The numerator and the denominator of 1/3 have no common divisor except 1
*
* @author AhmedNasser
*/
public class Rational extends Number implements Comparable<Rational> {
// Data fields for numerator and denominator
private long numerator = 0;
private long denominator = 1;
/** Construct a rational with default properties */
public Rational() {
this(0, 1);
}
/** Construct a rational with specified numerator and denominator
* @param numerator the numerator
* @param denominator the denominator
*/
public Rational (long numerator, long denominator) {
int gcd = gcd(numerator, denominator);
this.numerator = ((denominator > 0 ? 1 : -1) * numerator / gcd);
this.denominator = Math.abs(denominator) / gcd;
}
/** Find two GCD for two numbers */
private static int gcd(long n, long d) {
long n1 = Math.abs(n);
long n2 = Math.abs(d);
int gcd = 1;
for (int k = 1; k <= n1 && k <= n2; k++)
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
return gcd;
}
/**
* Get the numerator
* @return the numerator
*/
public long getNumerator() {
return numerator;
}
/**
* Get the denominator
* @return the denominator
*/
public long getDenominator() {
return denominator;
}
/**
* Add a rational number to this rational
* @param secondRational the added rational
* @return the new rational after the adding
*/
public Rational add(Rational secondRational) {
long n = numerator * secondRational.getDenominator() +
secondRational.getNumerator() * denominator;
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}
/**
* Subtract a rational from this rational
* @param secondRational the subtracted rational
* @return the new rational after the subtraction
*/
public Rational subtract(Rational secondRational) {
long n = numerator * secondRational.getDenominator() -
secondRational.getNumerator() * denominator;
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}
/**
* Multiply a rational number by this rational
* @param secondRational the multiplied rational
* @return the new rational after the multiplying
*/
public Rational multiply(Rational secondRational) {
long n = numerator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}
/**
* Divide a rational number by this rational
* @param secondRational the divided rational
* @return the new rational after the dividing
*/
public Rational divide(Rational secondRational) {
long n = numerator * secondRational.getDenominator();
long d = denominator * secondRational.getNumerator();
return new Rational(n, d);
}
@Override
public String toString() {
if (denominator == 1)
return numerator + "";
else
return numerator + "/" + denominator;
}
@Override
public boolean equals(Object o) {
return this.subtract((Rational) o).getNumerator() == 0;
}
// Implement the intValue method in Number
@Override
public int intValue() {
return (int) doubleValue();
}
// Implement the floatValue method in Number
@Override
public float floatValue() {
return (float) doubleValue();
}
// Implement the doubleValue method in Number
@Override
public double doubleValue() {
return numerator * 1.0 / denominator;
}
// Implement the loneValue method in Number
@Override
public long longValue() {
return (long) doubleValue();
}
@Override
public int compareTo(Rational secondRationa) {
if (this.subtract(secondRationa).getNumerator() > 0)
return 1;
else if (this.subtract(secondRationa).getNumerator() < 0)
return -1;
else
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment