简体   繁体   中英

How to represent mixed fractions in java

There is a way to represent fractions over here . Is there a clear abstraction to represent mixed fractions, so that you can perform addition, subtraction, multiplication operations on it.

A mixed fraction is only a display difference and not a mathematical one. You can just convert it to mixed when you print the answer.

你也许可以通过扩展Fraction做到这一点:你可能想要添加一个新的构造函数,除了分子/分母new MixedFraction(2,3,4) 2 3/4之外还需要一个整数,并覆盖toString以显示所需的格式。

Well, I think you can turn it into an improper fraction by adding the fraction part with the integer, do the operations using the methods mentioned in the page you linked to, and transform it back to a mixed fraction by doing this:

String turnImproperFractionToMixedFraction(Fraction f) {
    int a = f.getNumerator() / f.getDenominator();
    int b = f.getNumerator() % f.getDenominator();
    return a != 0 ? (a + " " + b + "/" + f.getDenominator()) : (b + "/" + f.getDenominator());
}

Using that we can do this:

Fraction f = new Fraction(8, 3);
System.out.println(turnImproperFractionToMixedFraction(f));

Which should print out 2 2/3

I think if you keep track of numbers as numerator and denominator (improper fractions - numerators larger than the denominators - as well) it simplifies your task:

public class myFraction {
    int numerator;
    int denominator;

    ... constructors and methods ...
}

Then when you want to do addition and subtraction, you can find the LCD faster, and you can work in one representation.

When you want to display it as a string, you can easily find the number of whole divisions [ie floor(numerator/denominator)] and you can find the remainder using either modulus:

numerator % denominator + "/" + denominator

or you can save the value you get from floor(numerator/denominator) and subtract it from the raw division, which will give you the fractional component (0 <= x < 1), ie

int myDivision = numerator/denominator;
int myFloor = floor(myDivision);

int fracComponent = myDivision - myFloor; //a number between 0 and 1
int mixedNumerator = fracComponent * denominator; //the numerator of your mixed part

the display is then like

myFloor + " " + mixedNumerator + "/" + denominator //i.e. 3 1/3

A calculator I used a long time ago when learning algebra had a mixed fraction display and showed the answer as 3u1/3 (for "units")

There are several ways where you can decrease your number of operations to get all the facts you want from a number. Are you doing manipulations on decimal numbers which you must turn into mixed fractions, or are you starting from scratch for input?

I don't know of any Java-based API that has this functionality built-in, but it's not too hard to implement yourself and definitely is a good exercise

看起来您可以使用FractionFormatProperFractionFormat (在您引用的相同Apache Commons包中)在Fraction对象和表示混合分数的String对象之间进行转换。

If you just want your fraction to display as a mixed fraction, try this!

     if ((numer >= denom) & (numer % denom == 0)){
        impFrac = numer / denom;
        System.out.print(numer);
        System.out.print("/"+denom);
        System.out.print(" is an improper fraction, and can be reduced to "+impFrac);
        System.out.println(".");
    }
    else if (numer > denom){
        impFrac = numer / denom;
        mixFrac = numer % denom;
        System.out.print(numer);
        System.out.print("/"+denom);
        System.out.print(" is an improper fraction, and its mixed fraction is "+impFrac);
        System.out.print(" and "+mixFrac);
        System.out.print("/"+denom);
        System.out.println(".");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM