Hi, so I had to make a Fraction class for a programming class. It was supposed t
ID: 3772540 • Letter: H
Question
Hi, so I had to make a Fraction class for a programming class. It was supposed to convert the fractions to a decimal, reduce them, and use the overloaded ==, <, <=, >, >= operators to compare the fractions. I submitted it and my professor said the following - "If you return an int/int it will return an int: 1/2=0.
You need to return 1.0*numerator/denominator.
The easiest way to compare two fractions is to compare the toDecimal values." I don't really understand what she wants me to do. I understand integer division truncates the remainder, but all of my variables are doubles, so I don't see why that's an issue. I have no clue what she means by using the toDecimal function in the overloaded operators. Here's all of my code.
Thank you!
Explanation / Answer
Hi buddy, I see some serious problem here. Just kidding. :)
First of all, kudos for writing this big object oriented program while you are learning programming. All logic is correct, except some language intricacies.
So, your professor told you that "If you return an int/int it will return an int: 1/2=0." and my friend, she is damn right. Ok you got it too that integer division truncates the remainder.
What you don't understand is when all of your variables are double, then what's the problem with this integer division, right?
Ok, let's take this.
double var = 12 / 5;
Ok, so compiler sees a 12, a division sign, and 5, divides 12 by 5. Answer it got is 2.4.
BUT it sees that both the operands are integers, so the result it returns must also be an integer, because you might not be rich enough to store a double value if it returned one. All it has seen with you (poor guy) are integers.
So, it truncates the remainder out of the result, and only returns the quotient, which is 2 in this case.
But wait a minute, while evaluating the right side expression, compiler did not even bother to see, what type of variable you have on the left side to collect the result. It couldn't see your richness. Now, when it has to store the result in to the variable on the left side, its eyes open. It sees a double variable, but now all it is left with is the quotient (2, here). So, to make you happy, it makes it a double like this, 2.0 and stores in the variable var. :)
So, if you have also shown the compiler a double variable (that you are rich) there on the right side, like your professor told, then it would have returned you a double output.
double var = 1.0 * 12/5
Ok, so that was a little irony about the compiler. But, in reality, the division operator (/) is actually to return you only the quotient in case of integers, and there is % operator to get the remainder in a division.
So, that's why your professor is asking you to compare the toDecimal values, and that too, when you have calculated those decimal values like above i.e. 1.0*numerator / denominator
Now, you are calculating the decimal values correctly using the function toDecimal i.e.
This is right. Another way to do it can be, when you explicitly tell the compiler to treat your numerator or denomitor to treat as double, and in return it will return the double result. So, it can also be like as following:
So, you cast the integer variable numerator to double, and now the compiler will return the double value.
At last, you are facing problem here
You want to compare two fractions, and don't know how to use toDecimal there. I will show you 3 ways. ;)
[1] First, explicitly cast one of the variables (numerator or denominator) on both the sides, or append a 1.0
or
[2] Convert both the sides to decimals, by using toDecimal function i.e.
I hope you get it. Happy learning :)
Please rate the answer if it helped you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.