The fractional class of Java

Source: Internet
Author: User

Overview

Fractional classes are important in the algorithm, and less important in Java, the Java base Class Library provides BigInteger, providing a similar approach,

package combination math; public class Fraction {private long numerator;//numerator private long denominator;//Denominator public fractio N (long numerator, long denominator) {this.        numerator = numerator;        if (denominator = = 0) {throw new ArithmeticException ("Denominator cannot be zero"); } else {this.        denominator = denominator;    } change ();    } public fraction () {This (0, 1);    } public Long Getnumerator () {return numerator;    } public void Setnumerator (long numerator) {numerator = numerator;    } public Long Getdenominator () {return denominator;    } public void Setdenominator (long denominator) {denominator = denominator; Private fraction Change () {Long gcd = THIS.GCD (this. Numerator, this.        denominator); This.        Numerator/= gcd; This.        Denominator/= gcd;    return this; }/** * Max common factor * * @param A * @param b * @return * * Private long gcd (long A, loNg b) {Long mod = a% B;        if (mod = = 0) {return b;        } else {return gcd (b, MoD); }}/** * arithmetic * @return */public fraction Add (fraction second) {Return to new fraction (this. Numerator * second. Denominator + second. Numerator * this. Denominator, this. Denominator * second.    denominator); The public fraction sub (fraction second) {Return to new fraction (this. Numerator * second. Denominator-second. Numerator * this. Denominator, this. Denominator * second.    denominator); Public fraction multiply (fraction second) {Return to new fraction (this. Numerator*second. Numerator, this. Denominator * second.    denominator); Public fraction devide (fraction second) {Return to new fraction (this. Numerator*second. Denominator, this. Denominator * second.    numerator); } @Override Public String toString () {return String.Format ("{%d/%d}", tHis. Numerator, this.    denominator);        } public static void Main (string[] args) {fraction a = new fraction (7, 32);        Fraction b = new fraction (13, 32);    System.out.println (A.add (b)); }}

Fractional classes for 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.