Rational Number class Java

Source: Internet
Author: User
Tags gcd

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.