-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRational.java
57 lines (47 loc) · 1.46 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
public class Rational
{
int myNumerator;
int myDenominator;
public Rational(int numerator, int denominator)
{
this.myNumerator = numerator;
this.myDenominator = denominator;
}
public Rational plus(Rational b)
{
int newNumerator = myNumerator * b.myDenominator + b.myNumerator * myDenominator;
int newDenominator = myDenominator * b.myDenominator;
Rational sum = new Rational(newNumerator, newDenominator);
return sum;
}
public Rational minus(Rational b)
{
int newNumerator = myNumerator * b.myDenominator - b.myNumerator * myDenominator;
int newDenominator = myDenominator * b.myDenominator;
Rational result = new Rational(newNumerator, newDenominator);
return result;
}
public Rational times(Rational b)
{
int newNumerator = myNumerator * b.myNumerator;
int newDenominator = myDenominator * b.myDenominator;
Rational result = new Rational(newNumerator, newDenominator);
return result;
}
public Rational over(Rational b)
{
// Swaping brøken
int newNumeratorA = myDenominator;
int newDenominatorA = myNumerator;
int newNumeratorB = b.myDenominator;
int newDenominatorB = b.myNumerator;
int newNumerator = newNumeratorA * newNumeratorB;
int newDenominator = newDenominatorA * newDenominatorB;
Rational result = new Rational(newNumerator, newDenominator);
return result;
}
public String toString()
{
return this.myNumerator + "/" + this.myDenominator;
}
}