forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRational.java
More file actions
executable file
·125 lines (123 loc) · 3.86 KB
/
Rational.java
File metadata and controls
executable file
·125 lines (123 loc) · 3.86 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
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
124
125
/* */ package ch19;
/* */
/* */ public class Rational
/* */ extends Number implements Comparable<Rational> {
/* 5 */ private long numerator = 0L;
/* 6 */ private long denominator = 1L;
/* */
/* */
/* */ public Rational() {
/* 10 */ this(0L, 1L);
/* */ }
/* */
/* */
/* */ public Rational(long numerator, long denominator) {
/* 15 */ long gcd = gcd(numerator, denominator);
/* 16 */ this.numerator = ((denominator > 0L) ? 1L : -1L) * numerator / gcd;
/* 17 */ this.denominator = Math.abs(denominator) / gcd;
/* */ }
/* */
/* */
/* */ private static long gcd(long n, long d) {
/* 22 */ long n1 = Math.abs(n);
/* 23 */ long n2 = Math.abs(d);
/* 24 */ int gcd = 1;
/* */
/* 26 */ for (int k = 1; k <= n1 && k <= n2; k++) {
/* 27 */ if (n1 % k == 0L && n2 % k == 0L) {
/* 28 */ gcd = k;
/* */ }
/* */ }
/* 31 */ return gcd;
/* */ }
/* */
/* */
/* */ public long getNumerator() {
/* 36 */ return this.numerator;
/* */ }
/* */
/* */
/* */ public long getDenominator() {
/* 41 */ return this.denominator;
/* */ }
/* */
/* */
/* */
/* */ public Rational add(Rational secondRational) {
/* 47 */ long n = this.numerator * secondRational.getDenominator() + this.denominator * secondRational.getNumerator();
/* 48 */ long d = this.denominator * secondRational.getDenominator();
/* 49 */ return new Rational(n, d);
/* */ }
/* */
/* */
/* */
/* */ public Rational subtract(Rational secondRational) {
/* 55 */ long n = this.numerator * secondRational.getDenominator() - this.denominator * secondRational.getNumerator();
/* 56 */ long d = this.denominator * secondRational.getDenominator();
/* 57 */ return new Rational(n, d);
/* */ }
/* */
/* */
/* */ public Rational multiply(Rational secondRational) {
/* 62 */ long n = this.numerator * secondRational.getNumerator();
/* 63 */ long d = this.denominator * secondRational.getDenominator();
/* 64 */ return new Rational(n, d);
/* */ }
/* */
/* */
/* */ public Rational divide(Rational secondRational) {
/* 69 */ long n = this.numerator * secondRational.getDenominator();
/* 70 */ long d = this.denominator * secondRational.numerator;
/* 71 */ return new Rational(n, d);
/* */ }
/* */
/* */
/* */ public String toString() {
/* 76 */ if (this.denominator == 1L) {
/* 77 */ return this.numerator + "";
/* */ }
/* 79 */ return this.numerator + "/" + this.denominator;
/* */ }
/* */
/* */
/* */ public boolean equals(Object other) {
/* 84 */ if (subtract((Rational)other).getNumerator() == 0L) {
/* 85 */ return true;
/* */ }
/* 87 */ return false;
/* */ }
/* */
/* */
/* */ public int intValue() {
/* 92 */ return (int)doubleValue();
/* */ }
/* */
/* */
/* */ public float floatValue() {
/* 97 */ return (float)doubleValue();
/* */ }
/* */
/* */
/* */ public double doubleValue() {
/* 102 */ return this.numerator * 1.0D / this.denominator;
/* */ }
/* */
/* */
/* */ public long longValue() {
/* 107 */ return (long)doubleValue();
/* */ }
/* */
/* */
/* */ public int compareTo(Rational o) {
/* 112 */ if (subtract(o).getNumerator() > 0L)
/* 113 */ return 1;
/* 114 */ if (subtract(o).getNumerator() < 0L) {
/* 115 */ return -1;
/* */ }
/* 117 */ return 0;
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch19/Rational.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/