forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Rational.java
135 lines (117 loc) · 3.7 KB
/
Rational.java
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
126
127
128
129
130
131
132
133
134
135
// File Rational.java
/* Fall 2007 CS 216 NOTES on Handout:
*
* MCO: See notes scattered in comments throughout this file
*
* For now, use the comments as a guideline for the conversion to C++
* Explanation the following concepts will become clearer in the next class:
* - pointers
* - when to use "new" and "this"
*
* NOTE 0: For now, remove all instances of "new" and "this"
*
* C++ REMINDERS:
*
* 1) For Input/Output:
*
* #include <iostream>
* using namespace std;
*
* 2) To print to screen: cout << varname1 << " " << endl;
* (endl means "end of line" or newline)
*
* 3) C++ signature for main (must not be in any class):
* int main()
*
* 4) C++ class syntax reminders:
*
* // Instead of modifying each member function,
* // just a section for each in the class
* public:
* private:
*
*/
public class Rational {
private int num; // the numerator
private int den; // the denominator
// create and initialize a new Rational object
public Rational(int numerator, int denominator) {
if (denominator == 0) {
System.out.println("Denominator is zero");
}
int g = gcd(numerator, denominator);
num = numerator / g;
den = denominator / g;
}
// return string representation of (this)
public void print() {
if (den == 1) {
// MCO: NOTE 1
// For conversion to C++
// use the following
// cout << num << "" << endl;
// We'll talk about C++ operator overloading later for
// operators such as the '+' symbol soon
System.out.println(num + "");
} else {
System.out.println(num + "/" + den);
}
}
// return (this * b)
public Rational times(Rational b) {
// MCO: NOTE 2
// More explanation on this next class, for now
// remove "new" and "this" for conversion to C++
// Do this for all "new" and "this" for the Rational class
// for this exercise
return new Rational(this.num * b.num, this.den * b.den);
}
// return (this + b)
public Rational plus(Rational b) {
int numerator = (this.num * b.den) + (this.den * b.num);
int denominator = this.den * b.den;
return new Rational(numerator, denominator);
}
// return (1 / this)
public Rational reciprocal() {
return new Rational(den, num);
}
// return (this / b)
public Rational divides(Rational b) {
return this.times(b.reciprocal());
}
/*************************************************************************
* Helper functions
*************************************************************************/
// return gcd(m, n)
private int gcd(int m, int n) {
if (0 == n)
return m;
else
return gcd(n, m % n);
}
/*************************************************************************
* Test client
*************************************************************************/
public static void main(String[] args) {
Rational x, y, z;
// 1/2 + 1/3 = 5/6
x = new Rational(1, 2);
y = new Rational(1, 3);
z = x.plus(y);
z.print();
// 8/9 + 1/9 = 1
x = new Rational(8, 9);
y = new Rational(1, 9);
z = x.plus(y);
z.print();
// 4/17 * 7/3 = 28/51
x = new Rational(4, 17);
y = new Rational(7, 3);
z = x.times(y);
z.print();
// 0/6 = 0
x = new Rational(0, 6);
x.print();
}
}