Java Small Example: the class Fraction__java of analog fractions

Source: Internet
Author: User

A while ago CSDN blog Strange Slow incomparable, today better.

Here is an example of a simulated fractional operation: the Fraction class. After the fractional operation, use GCD to divide the numerator denominator. So there is also an example of gcd by the Euclidean method. In addition, if the denominator is zero when the fraction object is constructed, the exception is thrown, which is also a necessary check.

public class Fractiontest {public static void main (string[] args) {fraction a = new fraction (7, 32); Fraction b = new fraction (13, 32); System.out.println (A + "+" + B + "=" + A.add (b) + "(" + A.add (b). Doublevalue () + ")"); System.out.println (A + "-" + B + "=" + A.minus (b) + "(" + A.minus (b). Doublevalue () + ")"); System.out.println (A + "*" + B + "=" + a.multiply (b) + "(" + a.multiply (b). Doublevalue () + ")"); System.out.println (A + "/" + B + "=" + a.devide (b) + "(" + a.devide (b). Doublevalue () + ")");} Fractional class Fraction {private int numerator;/numerator private int denominator;/denominator fraction (int numerator, int denominator {if (denominator = 0) {throw new IllegalArgumentException ("Denominator cannot be 0"); this.numerator = numerator; this.denominator = denominator; Shrink (); Fraction () {This (0, 1);} public int getnumerator () {return numerator;} public void setnumerator (int numerator) {thi S.numerator = numerator; public int Getdenominator () {return denominator;} PublIC void setdenominator (int denominator) {this.denominator = denominator}//numerator denominator divided by gcd private fraction shrink () {in T maxcommondivisor = Getmaxcommondivisor (This.denominator, this.numerator); This.numerator/= Maxcommondivisor; This.denominator/= Maxcommondivisor; return this; }//Euclidean algorithm gcd private int getmaxcommondivisor (int a, int b) {int mod = a% B; if (mod = 0) {return b;} else {retur n getmaxcommondivisor (b, MoD); }//Fractional addition public fraction add (fraction this) {return new fraction (This.numerator * that.denominator + this.denominator * That.numerator, This.denominator * that.denominator); }//Fractional subtraction public fraction minus (fraction.) {return new fraction (This.numerator * that.denominator-this.denominator * That.numerator, This.denominator * that.denominator); }//Fractional multiplication public fraction multiply (fraction.) {return new fraction (This.numerator * that.numerator, This.denominator * That.denominator); //Fractional Division public fraction devide (fraction.) {return new FrAction (This.numerator * that.denominator, This.denominator * that.numerator); Public double Doublevalue () {return (double) Numerator/denominator} @Override public String toString () {return Str Ing.format ("{%d/%d}", This.numerator, This.denominator); } }

Run the output:

{7/32} + {13/32} = {5/8} (0.625)
{7/32}-{13/32} = { -3/16} (-0.1875)
{7/32} * {13/32} = {91/1024} (0.0888671875)
{7/32}/{13/32} = {7/13} (0.5384615384615384)

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.