Задача
Напишите класс для работы с не изменяемыми (immutable) рациональными дробями используя статические методы.
Код
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
import java.lang.*; class Rational { private int n, d; /** * @param n is a numerator of Fraction * @param d is a divider of Fraction */ public Rational(int n, int d) throws ArithmeticException { if (d == 0) throw new ArithmeticException("wrong "); int divisor = gcd(n, d); this.n = n / divisor; this.d = d / divisor; } /** * @param n is a numerator of Fraction * @param d is a divider of Fraction * @return the greatest common divisor of numerator and divider */ private static int gcd(int n, int d) { return (d == 0 ? n : gcd(d, n % d)); } /** * Check fractions for equality * * @param x the first fraction * @param y the seconds fraction * @return true if they are equals, otherwise returns false */ public static boolean equals(Rational x, Rational y) { if (x.n != y.n || x.d != y.d) return false; else return true; } /** * Add two Fractions x and y * * @param x the first Fraction * @param y the second Fraction * @return the result of there addition */ public static Rational add(Rational x, Rational y) { return new Rational(x.n * y.d + y.n * x.d, x.d * y.d); } /** * Subtract two Franctions x and y * * @param x the first Fraction * @param y the second Fraction * @return the result of there subtraction */ public static Rational sub(Rational x, Rational y) { return new Rational(x.n * y.d - y.n * x.d, x.d * y.d); } /** * Multiply two Fractions x and y * * @param x the first Fraction * @param y the seconds Fraction * @return the result of there multiplication */ public static Rational mul(Rational x, Rational y) { return new Rational(x.n * y.n, x.d * y.d); } /** * Divide two Fractions x and y * * @param x the first Fraction * @param y the second Fraction * @return the result of there division */ public static Rational div(Rational x, Rational y) { return new Rational(x.n * y.d, x.d * y.n); } /** * Convert fraction to string value * * @return fraction */ public String toString() { return n + "/" + d; } /** * Compare two fractions and write to console a result * * @param x x the first Fraction * @param y the second Fraction */ public static void cmp(Rational x, Rational y) { if (!equals(x, y)) { if ((x.n * y.d) > (y.n * x.d)) System.out.println(x.toString() + " > " + y.toString()); else System.out.println(y.toString() + " > " + x.toString()); } else System.out.println(x.toString() + " = " + y.toString()); } public static void main(String[] args) { try { Rational a = new Rational(4, 5); Rational b = new Rational(1, 2); Rational c = new Rational(2, 5); System.out.println("Equality - " + equals(a, b)); System.out.println("Equality - " + equals(mul(a, b), c)); System.out.println(c.toString() + "+" + b.toString() + "=" + add(c, b)); System.out.println(a.toString() + "-" + b.toString() + "=" + sub(a, b)); System.out.println(b.toString() + "-" + a.toString() + "=" + sub(b, a)); System.out.println(a.toString() + "*" + b.toString() + "=" + mul(c, b)); System.out.println(a.toString() + "%" + b.toString() + "=" + div(a, b)); cmp(a, b); cmp(b, c); cmp(mul(a, b), c); } catch (ArithmeticException e) { System.out.println("dividing on 0"); } } } |
Тест
Входящие данные | Операция | Выходящие данные |
4/5
1/2 |
проверка равенства | false |
4/5
1/2 2/5 |
сравнение по равенству дроби и произведения двух других | true |
2/5
1/2 |
сложение | 9/10 |
4/5
1/2 |
вычитание | 3/10 |
4/5
1/2 |
вычитание | -3/10 |
2/5
1/2 |
умножение | 1/5 |
4/5
1/2 |
деление | 8/5 |
4/5
1/2 |
сравнение | 4/5 > 1/2 |
4/5
1/2 |
сравнение | 1/2 > 2/5 |
4/5
1/2 2/5 |
сравнение дроби и произведения двух других | 2/5 = 2/5 |