-
Notifications
You must be signed in to change notification settings - Fork 36
/
Rational.scala
59 lines (48 loc) · 1.65 KB
/
Rational.scala
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
package week2
class Rational(x: Int, y: Int) { //the constructor
require(y!=0, "denominator must be nonzero")
//to guard against users creating illegal Rational with a zero denominator
def this(x: Int) = this(x,1) //a secondary constructor
private def gcd(a: Int, b: Int): Int = if(b==0) a else gcd(b, a % b)
private def abs(a: Int) = if(a>=0) a else -a
private val g = abs(gcd(x,y))
//We want to simplify Ints before we perform operations because the limit of ints
//are just about 2 billion. We don't want to get into arithemetic overflows early
val numer = x / g
val denom = y / g
//ADDITION
def add(that: Rational): Rational = {
new Rational(
this.numer*that.denom + that.numer*this.denom,
this.denom*that.denom
)
}
def + (that: Rational): Rational = {
new Rational(
this.numer*that.denom + that.numer*this.denom,
this.denom*that.denom
)
}
//NEGATE
def neg: Rational = new Rational(-numer, denom)
def unary_- :Rational = new Rational(-numer, denom) //prefix operation
//SUBTRACTION
def sub(that: Rational): Rational = add(that.neg)
def - (that: Rational): Rational = this + -that
//COMPARISON
def less(that: Rational): Boolean = {
this.numer*that.denom < that.numer*this.denom
}
def < (that: Rational): Boolean = {
this.numer*that.denom < that.numer*this.denom
}
//MAX
def max(that: Rational): Rational = {
if(this.less(that)) that else this
}
def max2(that: Rational): Rational = {
if(this < that) that else this
}
//A toString method of a class will always be called whenever we println
override def toString = numer + "/" + denom
}