-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rational.java
93 lines (79 loc) · 2.36 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
/*
Rational numbers are numbers that can be represented as a fraction p/q where p is an integer number and q is a positive integer (q != 0).
Design and implement an Abstract Data Type (ADT) Rational for representing such numbers. Implement methods to add and multiply rational numbers.
Implement a method for return the value of a rational number as a double value. Make sure that the numerator p and denominator q do not have common divisors in your implementation.
Use the algorithm for calculation of the greatest common divisor to ensure this property.
public class Rational {
// return p/q as double value
public double abs()
// negate a rational number and return as new a Rational object
public Rational negate()
// add this and b and return as a new rational number
public Rational plus(Rational b)
// compute this += b and return this
public Rational plusEq(Rational b)
public Rational multiply(Rational b)
public Rational multiplyEq(Rational b)
// convert this rational number to String
public String toString()
}
*/
public class Rational
{
private int p, q;
public Rational(int P0,int Q0)
{
int commonDiv = gcd(P0,Q0);
p = P0/commonDiv;
q = Q0/commonDiv;
}
public static int gcd(int a,int b)
{
if(b == 0) return a;
return gcd(b,a%b);
}
public double abs()
{
return (double)p/q;
}
public Rational negate()
{
return new Rational((-1*p),q);
}
public Rational plus(Rational b)
{
return new Rational((p*b.q) + (q*b.p), q*b.q);
}
public Rational plusEq(Rational b)
{
p = (p*b.q) + (q*b.p);
q = q*b.q;
return this;
}
public Rational multiply(Rational b)
{
return new Rational(p*b.p,q*b.q);
}
public Rational multiplyEq(Rational b)
{
p = p*b.p;
q = q*b.q;
return this;
}
public String toString()
{
return (p + "/" + q);
}
public static void main(String[] args)
{
Rational a = new Rational(40,80);
Rational b = new Rational(3,8);
System.out.println(a);
System.out.println(a.abs());
System.out.println(a.negate());
System.out.println(a.plus(b));
System.out.println(a.plusEq(b));
System.out.println(a.multiply(b));
System.out.println(a.multiplyEq(b));
}
}