An immutable class never changes after it is built, which makes it safe to share. Write one for exact fractions.
What to build
A Fraction class that adds, subtracts, multiplies and compares fractions exactly.
Requirements
- Write an immutable class
Fractionwith private finallongnumerator and denominator, always in lowest terms with a positive denominator, and methodsplus,minus,times(each returning a new Fraction) andcompareTo. toString()givesN/D, or justNwhen the denominator is 1.- Each line is
A/B OP C/Dwith OP one of+,-,*,?. Print the result, or for?printA/B < C/D,A/B = C/DorA/B > C/Dusing the reduced forms. A zero denominator in the input printsinvalid fraction.
Input
Line 1: n. Then n lines.
Output
n lines.
Constraints
1 ≤ n ≤ 50; numerators and denominators have absolute value at most 10^6.
Example
Input:
4
1/2 + 1/3
3/4 - 1/4
2/3 * 9/4
1/2 ? 2/4
Output:
5/6
1/2
3/2
1/2 = 1/2
Why it works
Reducing in the constructor means every Fraction is always in its simplest form, so toString and comparisons never need to think about it, and returning new objects means a fraction once made can be trusted not to change.
Examples
4 1/2 + 1/3 3/4 - 1/4 2/3 * 9/4 1/2 ? 2/4
5/6 1/2 3/2 1/2 = 1/2
2 1/0 + 1/2 -6/-8 ? 1/2
invalid fraction 3/4 > 1/2
Hints
Hint 1
Store fractions in lowest terms with a positive denominator: divide by the gcd in the constructor, and move a minus sign to the numerator.
Hint 2
add and multiply return new Fraction objects; nothing ever changes an existing one.
Solution
Show one correct solution (try it yourself first)
import java.util.Scanner;
class Fraction {
private final long num, den;
Fraction(long num, long den) {
if (den == 0) throw new IllegalArgumentException("invalid fraction");
if (den < 0) {
num = -num;
den = -den;
}
long g = gcd(Math.abs(num), den);
this.num = num / g;
this.den = den / g;
}
private static long gcd(long a, long b) {
return b == 0 ? (a == 0 ? 1 : a) : gcd(b, a % b);
}
static Fraction parse(String s) {
String[] p = s.split("/");
return new Fraction(Long.parseLong(p[0]), Long.parseLong(p[1]));
}
Fraction plus(Fraction o) {
return new Fraction(num * o.den + o.num * den, den * o.den);
}
Fraction minus(Fraction o) {
return new Fraction(num * o.den - o.num * den, den * o.den);
}
Fraction times(Fraction o) {
return new Fraction(num * o.num, den * o.den);
}
int compareTo(Fraction o) {
return Long.compare(num * o.den, o.num * den);
}
@Override
public String toString() {
return den == 1 ? String.valueOf(num) : num + "/" + den;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i < n; i++) {
String a = in.next(), op = in.next(), b = in.next();
try {
Fraction x = Fraction.parse(a), y = Fraction.parse(b);
switch (op) {
case "+" -> System.out.println(x.plus(y));
case "-" -> System.out.println(x.minus(y));
case "*" -> System.out.println(x.times(y));
default -> {
int c = x.compareTo(y);
System.out.println(x + (c < 0 ? " < " : c == 0 ? " = " : " > ") + y);
}
}
} catch (IllegalArgumentException e) {
System.out.println("invalid fraction");
}
}
}
}