So I'm a student that's learning Java. I am currently busy with an assignment, but I keep getting stuck with a certain bit of if-else logic.
My assignment requests that I should write a class file that takes a rational number (e.g. 72 / 14), display it, break it down its lowest form ( 36 / 7 in this case), display the rational number as a double
value, and finally display the biggest common factor. What I'm getting stuck on is the fact the my code doesn't sift out the fact that for example, 72 / 13 is the smallest the rational number can become, but I got it so it won't crash, but in turn I traded it off for 72 / 14's value searching and such not working correctly.
My code:
public class QTwoAssiThree
{
private int numerator;
private int denominator;
public void writeOutput()
{
if (getGCD(numerator, denominator) >= 2)
{
System.out.println(toString() + " is the simplified rational number.");
System.out.println(getValue() + " is the value of the two rational numbers as one number.");
System.out.println((getGCD(numerator, denominator) * 2) + " is the largest common factor.");
}
else if (getGCD(numerator, denominator) == 1)
{
System.out.println(getValue() + " is the value of the two rational numbers as one number.");
System.exit(0);
}
}
public QTwoAssiThree()
{
numerator = 0;
denominator = 0;
}
public QTwoAssiThree(int initialNum, int initialDenom)
{
if (denominator % 2 == 0)
{
numerator = initialNum;
if (initialDenom > 0)
{
denominator = initialDenom;
System.out.println(toString() + " is the rational number entered.");
simplify(numerator, denominator);
}
else
{
System.out.println("Invalid value added as the denominator! Only positive integers should be used.");
System.exit(0);
}
}
else if (denominator % 2 == 1)
{
System.out.println(toString() + " is what was entered, and is already in its simplest form.");
}
}
private void simplify(int newNum, int newDenom)
{
numerator = newNum / getGCD(newNum, newDenom);
denominator = newDenom / getGCD(newNum, newDenom);
}
private static int getGCD(int x, int y)
{
int difference = x % y;
return difference;
}
private double getValue()
{
double doubleValue = (double)numerator / (double)denominator;
return doubleValue;
}
public String toString()
{
String fraction = Integer.toString(numerator) + "/" + Integer.toString(denominator);
return fraction;
}
}
Now, please ignore the fact that I'm still somewhat low on the knowledgeable Java programmer scale. I would appreciate any pointers on this, as I got stuck and can't really figure out how to set it up. There's two places where I used if-else
to see if it would work, the first being in:
public void writeOutput()
and in the public QTwoAssiThree(int initialNum, int initialDenom)
constructor. Initially there weren't any if-else
statements there, code did work though, but not for uneven integers.
public class QTwoAssiThreeDemo
{
public static void main(String[] args)
{
QTwoAssiThree rationalNumbers = new QTwoAssiThree(72, 14);
rationalNumbers.writeOutput();
}
}
is used to demonstrate the whole class.
Basically what my outputs should look like is as follow:
72/14 is the rational number entered.
36/7 is the simplified rational number.
5.142857143 is the value of the two rational numbers as one number.
2 is the largest common factor.
and
72/13 is what was entered, and is already in its simplest form.
5.538461538 is the value of the two rational numbers as one number.
The code is unoptimized and looks ugly, I know, but any help would be appreciated, I will do a run through it all to make it look a bit nicer before I submit the assignment.