I\'m trying to make this rule for my fractions program: - Negative signs should
ID: 3621969 • Letter: I
Question
I'm trying to make this rule for my fractions program:- Negative signs should only appear in the numerator (Ex: -2/3 is acceptable but not 2/-3)
- Anything with 0 in the numerator & non-zero denominator (Ex: 0/-1 or 0/2 - should reduce to 0/1)
- Fractions never have 0 in the denominator. If such a Fraction is about to be constructed, the constructor prints a warning message and changes it to 0/1.
I don't know what to add in my public static Fraction read ( PrintStream ps, Scanner sc ) or my public String toString() for these rules above. I got everything so that it prints out a numerator/denominator when i input the two but thats it.
Explanation / Answer
// - Negative signs should only appear in the numerator (Ex: -2/3 is acceptable but not 2/-3)
if(denominator < 0)
{
denominator = - denominator;
numerator = - numerator;
}
// - Anything with 0 in the numerator & non-zero denominator (Ex: 0/-1 or 0/2 - should reduce to 0/1)
if(numerator == 0)
{
denominator = 1;
}
// - Fractions never have 0 in the denominator. If such a Fraction is about to be constructed, the constructor prints a warning message and changes it to 0/1.
if(denominator == 0)
{
numerator = 0;
denominator = 1;
}