How to represent mixed fractions in java

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

+4
source share
6 answers

Perhaps you can do this by expanding Fraction : you can add a new constructor that takes an integer in addition to the numerator / denominator new MixedFraction(2,3,4) for 2 3/4 and overrides toString to display in the desired format.

+6
source

The mixed fraction represents only the difference in display, not the mathematical one. You can just convert it to mixed when you type the answer.

+9
source

Well, I think you can turn it into the wrong fraction by adding part of the fraction with an integer, perform the operations using the methods mentioned on the page you are attached to, and convert it back to a mixed fraction by doing the following:

 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 this, we can do this:

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

For printout 2 2/3

+3
source

I think that if you keep track of numbers as the numerator and denominator (also the wrong fractions - numerators larger than the denominators), this will simplify 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 view.

If you want to display it as a string, you can easily find the number of integer divisions [t. (numerator / denominator)], and you can find the remainder using either the module:

 numerator % denominator + "/" + denominator 

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

 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 then looks like

 myFloor + " " + mixedNumerator + "/" + denominator //ie 3 1/3 

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

There are several ways to reduce the number of operations in order to get all the facts you need from a number. Do you do decimal manipulations that you have to turn into mixed fractions, or do you start from scratch for input?

I don’t know any Java-API that has built-in functionality, but it’s not too difficult to implement myself and, of course, is a good exercise

+1
source

It looks like you could use FractionFormat or ProperFractionFormat (in the same Apache Commons package you referenced) to convert between Fraction objects and String objects representing mixed fractions.

+1
source

If you want your fraction to appear 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("."); 
0
source

All Articles