This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def makeString(r: Rational) = | |
| r.numer + "/" + r.denom |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rational(x:Int, y:Int) | |
| { | |
| def numer = x | |
| def denom = y | |
| def add(that: Rational) = | |
| new Rational( | |
| numer * that.denom + that.numer * denom, | |
| denom * that.denom) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rational(x:Int, y:Int) | |
| { | |
| def numer = x | |
| def denom = y | |
| def add(that: Rational) = | |
| new Rational( | |
| numer * that.denom + that.numer * denom, | |
| denom * that.denom) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package classes | |
| object rationals { | |
| val x = new Rational(1,2) | |
| val y = new Rational(2,3) | |
| x.add(y) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package classes | |
| object rationals { | |
| val x = new Rational(1,3) | |
| val y = new Rational(5,7) | |
| val z = new Rational(3,2) | |
| x.sub(y).sub(z) | |
| } | |
| class Rational(x:Int, y:Int) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package classes | |
| object rationals { | |
| val x = new Rational(1,3) | |
| val y = new Rational(5,7) | |
| val z = new Rational(3,2) | |
| y.add(y) | |
| } | |
| class Rational(x:Int, y:Int) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rational(x:Int, y:Int) | |
| { | |
| private def gcd(a: Int, b:Int): Int = if (b == 0) a else gcd(b, a%b) | |
| private val g = gcd(x,y) | |
| def numer = x / g | |
| def denom = y / g | |
| def less(that: Rational) = numer * that.denom < that.numer * denom | |
| def add(that: Rational) = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package classes | |
| object rationals { | |
| val x = new Rational(1,3) | |
| val y = new Rational(5,7) | |
| val z = new Rational(3,2) | |
| x.less(y) | |
| x.max(y) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package classes | |
| object rationals { | |
| val x = new Rational(1,3) | |
| val y = new Rational(5,7) | |
| val z = new Rational(3,2) | |
| x - y - z | |
| x + y | |
| y + y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object scratch{ | |
| new week3.Rational(1,2) | |
| } |