public class Rational extends number implements comparable {
Private Long numerator;//molecule
Private long denominator;//Denominator
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Rational rational1 = new Rational (1, 10);
Rational rational2 = new Rational (1,-10);
System.out.println (Rational1.add (Rational2));
System.out.println (Rational1.subtract (Rational2));
System.out.println (Rational1.multiple (Rational2));
System.out.println (Rational1.divide (Rational2));
}
Public Rational () {
TODO auto-generated Constructor stub
This (0, 1);
}
Public Rational (long numerator, long denominator) {
Long gcd = gcd (numerator, denominator);
This.numerator = (Denominator > 0? 1:-1) * NUMERATOR/GCD;
This.denominator = Math.Abs (denominator)/gcd;
}
public static long gcd (long A, long B) {
Long N1 = Math.Abs (a);
Long N2 = Math.Abs (b);
Long remainder = n1% N2;
while (Remainder > 0) {
N1 = n2;
N2 = remainder;
remainder = n1% N2;
}
return n2;
}
Public long Getnumerator () {
return numerator;
}
Public long Getdenominator () {
return denominator;
}
Public rational Add (rational secondrational) {
Long n = numerator * Secondrational.getdenominator () + Denominator
* Secondrational.getnumerator ();
Long d = denominator * secondrational.getdenominator ();
return new Rational (n, D);
}
Public rational subtract (rational secondrational) {
Long n = numerator * Secondrational.getdenominator ()-Denominator
* Secondrational.getnumerator ();
Long d = denominator * secondrational.getdenominator ();
return new Rational (n, D);
}
Public rational multiple (rational secondrational) {
Long n = numerator * Secondrational.getnumerator ();
Long d = denominator * secondrational.getdenominator ();
return new Rational (n, D);
}
Public rational divide (rational secondrational) {
Long n = numerator * Secondrational.getdenominator ();
Long d = denominator * secondrational.getnumerator ();
return new Rational (n, D);
}
@Override
public boolean equals (Object obj) {
TODO auto-generated Method Stub
if (this.subtract (Rational) obj). Getnumerator () = = 0) {
return true;
}
else {
return false;
}
}
@Override
Public String toString () {
TODO auto-generated Method Stub
if (denominator = = 1) {
return string.valueof (numerator);
}
else {
return numerator + "/" + denominator;
}
}
@Override
public int intvalue () {
TODO auto-generated Method Stub
return (int) doublevalue ();
}
@Override
Public long Longvalue () {
TODO auto-generated Method Stub
Return (long) doublevalue ();
}
@Override
public float Floatvalue () {
TODO auto-generated Method Stub
return (float) doublevalue ();
}
@Override
Public double Doublevalue () {
TODO auto-generated Method Stub
return numerator * 1.0/denominator;
}
@Override
public int compareTo (Object o) {
TODO auto-generated Method Stub
if (This.subtract (Rational) o). Getnumerator () > 0) {
return 1;
}
else if (this.subtract (Rational) o). Getnumerator () < 0) {
return-1;
}
else {
return 0;
}
}
}
Rational Number class Java