patternjavaMinor
Converting fractions to mixed numbers
Viewed 0 times
convertingnumbersfractionsmixed
Problem
I've been making a program for an assignment that converts fractions to mixed numbers, and I'm done, but I was wondering if I could simplify it in any way, since it seems quite complicated right now.
Also, keep in mind that I am not allowed to use any
I realize this is an assignment, and it's fine if you choose not to give me an answer. In fact, I'd actually prefer to have some tips or hints to help figure out how to solve it, rather than just telling me the answer.
Also, the 'c.print' is basically a print statement. I'm importing/using a different custom class, which is why it is different from the normal print statement.
```
int numerator = 0; //can change to any value
int denominator = -2; //can change to any value
public int[] reducedFraction (int numerator, int denominator)
{
int GCD = numerator;
int tempN = numerator;
int tempD = denominator;
int[] fractionParts = {0, numerator, denominator};
if (numerator == 0) //if the numerator is 0
{
return fractionParts;
}
//Making sure the program works with negative values by temporarily converting GCD and fractionParts[2] to positive values
GCD = (numerator tempD)
GCD -= tempD;
else
tempD -= GCD;
}
//Simplfying numerator and denominator
fractionParts [1] /= GCD;
fractionParts [2] /= GCD;
//Temporarily making the numerator and denominator positive
tempN = (fractionParts [1] < 0) ? -fractionParts [1]:
fractionParts [1];
tempD = (fractionParts [2] < 0) ? -fractionParts [2]:
fractionParts [2];
//if the numerator is not greater than the denominator return the simplified fraction.
if (tempN < tempD)
{
return fractionParts;
}
//Finding value of whole number, numerator and possibly converting denominator to original sign
fra
Also, keep in mind that I am not allowed to use any
Math or String methods to do this assignment, and I can only use +, -, *, and / as arithmetic operators. I also cannot use % or a modulus function.I realize this is an assignment, and it's fine if you choose not to give me an answer. In fact, I'd actually prefer to have some tips or hints to help figure out how to solve it, rather than just telling me the answer.
Also, the 'c.print' is basically a print statement. I'm importing/using a different custom class, which is why it is different from the normal print statement.
```
int numerator = 0; //can change to any value
int denominator = -2; //can change to any value
public int[] reducedFraction (int numerator, int denominator)
{
int GCD = numerator;
int tempN = numerator;
int tempD = denominator;
int[] fractionParts = {0, numerator, denominator};
if (numerator == 0) //if the numerator is 0
{
return fractionParts;
}
//Making sure the program works with negative values by temporarily converting GCD and fractionParts[2] to positive values
GCD = (numerator tempD)
GCD -= tempD;
else
tempD -= GCD;
}
//Simplfying numerator and denominator
fractionParts [1] /= GCD;
fractionParts [2] /= GCD;
//Temporarily making the numerator and denominator positive
tempN = (fractionParts [1] < 0) ? -fractionParts [1]:
fractionParts [1];
tempD = (fractionParts [2] < 0) ? -fractionParts [2]:
fractionParts [2];
//if the numerator is not greater than the denominator return the simplified fraction.
if (tempN < tempD)
{
return fractionParts;
}
//Finding value of whole number, numerator and possibly converting denominator to original sign
fra
Solution
First, a couple of tips:
-
Using an
-
The handling of negative numbers is very poor. You could determine the sign of the number in one place early on, use it to multiply the integral part and that's it. That would get rid of many confusing redundant logic. The current code also has a bug related to it: for the input
-
An elegant implementation of the
-
The printing of the number can be cleaner and simpler using
I recommend to rewrite the function in terms of this class:
Rather than printing the output, use unit tests to verify.
After you finished the implementation above,
with the bug that I mentioned fixed,
these unit tests should pass:
Whatever IDE you use, there is certainly an easy way to add a JUnit4 test class and run it. Give it a try!
-
Using an
int[] to represent the result is a poor choice. It would be better to create a class for this, for example called MixedNumber. Then, instead of accessing the elements by indexes, you can access them by field name. It could also have an appropriate toString method to print itself-
The handling of negative numbers is very poor. You could determine the sign of the number in one place early on, use it to multiply the integral part and that's it. That would get rid of many confusing redundant logic. The current code also has a bug related to it: for the input
(3, -2) it gives back -1 1/-2 instead of -1 1/2-
An elegant implementation of the
gcd algorithm is this recursive function: return b == 0 ? a : gcd(b, a % b);-
The printing of the number can be cleaner and simpler using
String.formatI recommend to rewrite the function in terms of this class:
class MixedNumber {
private final int integral;
private final int numerator;
private final int denominator;
private MixedNumber(int integral, int numerator, int denominator) {
this.integral = integral;
this.numerator = numerator;
this.denominator = denominator;
}
private static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static MixedNumber fromFraction(int numerator, int denominator) {
// TODO: simplified main implementation comes here
}
@Override
public String toString() {
if (numerator == 0) {
return "" + integral;
} else if (integral == 0) {
return String.format("%d/%d", numerator, denominator);
} else {
return String.format("%d %d/%d", integral, numerator, denominator);
}
}
}Rather than printing the output, use unit tests to verify.
After you finished the implementation above,
with the bug that I mentioned fixed,
these unit tests should pass:
@Test
public void testZeroNumerator() {
assertEquals("0", MixedNumber.fromFraction(0, -2).toString());
}
@Test
public void test_minus3_over_2() {
assertEquals("-1 1/2", MixedNumber.fromFraction(-3, 2).toString());
}
@Test
public void test_3_over_minus2() {
assertEquals("-1 1/2", MixedNumber.fromFraction(3, -2).toString());
}
@Test
public void test_minus3_over_minus2() {
assertEquals("1 1/2", MixedNumber.fromFraction(-3, -2).toString());
}
@Test
public void test_123_over_82() {
assertEquals("1 1/2", MixedNumber.fromFraction(123, 82).toString());
}
@Test
public void test_82_over_123() {
assertEquals("2/3", MixedNumber.fromFraction(82, 123).toString());
}Whatever IDE you use, there is certainly an easy way to add a JUnit4 test class and run it. Give it a try!
Code Snippets
class MixedNumber {
private final int integral;
private final int numerator;
private final int denominator;
private MixedNumber(int integral, int numerator, int denominator) {
this.integral = integral;
this.numerator = numerator;
this.denominator = denominator;
}
private static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static MixedNumber fromFraction(int numerator, int denominator) {
// TODO: simplified main implementation comes here
}
@Override
public String toString() {
if (numerator == 0) {
return "" + integral;
} else if (integral == 0) {
return String.format("%d/%d", numerator, denominator);
} else {
return String.format("%d %d/%d", integral, numerator, denominator);
}
}
}@Test
public void testZeroNumerator() {
assertEquals("0", MixedNumber.fromFraction(0, -2).toString());
}
@Test
public void test_minus3_over_2() {
assertEquals("-1 1/2", MixedNumber.fromFraction(-3, 2).toString());
}
@Test
public void test_3_over_minus2() {
assertEquals("-1 1/2", MixedNumber.fromFraction(3, -2).toString());
}
@Test
public void test_minus3_over_minus2() {
assertEquals("1 1/2", MixedNumber.fromFraction(-3, -2).toString());
}
@Test
public void test_123_over_82() {
assertEquals("1 1/2", MixedNumber.fromFraction(123, 82).toString());
}
@Test
public void test_82_over_123() {
assertEquals("2/3", MixedNumber.fromFraction(82, 123).toString());
}Context
StackExchange Code Review Q#69548, answer score: 4
Revisions (0)
No revisions yet.