Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent mixed fractions in java

Tags:

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.

like image 900
Jason Avatar asked Nov 30 '22 09:11

Jason


2 Answers

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.

like image 187
JOTN Avatar answered Jan 02 '23 14:01

JOTN


You could perhaps do this by extending Fraction: you might want to add a new constructor that takes a whole number in addition to the numerator/denominator new MixedFraction(2,3,4) for 2 3/4, and override toString to display in the desired format.

like image 24
David Gelhar Avatar answered Jan 02 '23 16:01

David Gelhar