-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppMathBook.java
More file actions
77 lines (65 loc) · 3.2 KB
/
AppMathBook.java
File metadata and controls
77 lines (65 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package mathbook;
import java.math.BigInteger;
/**
* Utility class to compare fractions using cross multiplication.
*/
public class AppMathBook {
/**
* Entry point. If four arguments are provided they are interpreted as two
* fractions in the form {@code num1 den1 num2 den2}. Otherwise a set of
* predefined examples is executed.
*/
public static void main(String[] args) {
if (args.length == 4) {
Fraction a = new Fraction(Long.parseLong(args[0]), Long.parseLong(args[1]));
Fraction b = new Fraction(Long.parseLong(args[2]), Long.parseLong(args[3]));
System.out.println(checkMayorMenorIgual(a, b));
return;
}
System.out.println("001: " + checkMayorMenorIgual(new Fraction(2, 3), new Fraction(1, 4)));
System.out.println("002: " + checkMayorMenorIgual(new Fraction(3, 5), new Fraction(7, 8)));
System.out.println("003: " + checkMayorMenorIgual(new Fraction(-1, 6), new Fraction(-1, 2)));
System.out.println("004: " + checkMayorMenorIgual(new Fraction(7, 9), new Fraction(21, 27)));
System.out.println("005: " + checkMayorMenorIgual(new Fraction(11, 4), new Fraction(12, 5)));
System.out.println("006: " + checkMayorMenorIgual(new Fraction(6, 4), new Fraction(18, 12)));
System.out.println("007: " + checkMayorMenorIgual(new Fraction(-7, 7), new Fraction(0, 1)));
System.out.println("008: " + checkMayorMenorIgual(new Fraction(-5, 10), new Fraction(13, 26)));
System.out.println("009: " + checkMayorMenorIgual(new Fraction(5, 2), new Fraction(1, 1)));
System.out.println("010: " + checkMayorMenorIgual(new Fraction(17, 6), new Fraction(3, 1)));
System.out.println("011: " + checkMayorMenorIgual(new Fraction(-3, 1), new Fraction(-39, 13)));
System.out.println("012: " + checkMayorMenorIgual(new Fraction(4, 3), new Fraction(4, 9)));
}
private static String checkMayorMenorIgual(Fraction a, Fraction b) {
return switch (a.compareTo(b)) {
case -1 -> "a: " + a + " ((<)) b: " + b;
case 0 -> "a: " + a + " ((=)) b: " + b;
case 1 -> "a: " + a + " ((>)) b: " + b;
default -> "ERROR"; // This should never occur
};
}
/**
* Simple immutable fraction record with a comparison method based on cross
* multiplication.
*/
public record Fraction(long numerator, long denominator) implements Comparable<Fraction> {
public Fraction {
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero");
}
if (denominator < 0) { // normalize sign
numerator = -numerator;
denominator = -denominator;
}
}
@Override
public int compareTo(Fraction other) {
BigInteger left = BigInteger.valueOf(numerator).multiply(BigInteger.valueOf(other.denominator));
BigInteger right = BigInteger.valueOf(other.numerator).multiply(BigInteger.valueOf(denominator));
return left.compareTo(right);
}
@Override
public String toString() {
return numerator + "/" + denominator;
}
}
}