forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADTFractionApp.java
More file actions
108 lines (81 loc) · 2.67 KB
/
ADTFractionApp.java
File metadata and controls
108 lines (81 loc) · 2.67 KB
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
public class ADTFractionApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ADTFraction f1 = new ADTFraction(3, 5);
f1.display();
ADTFraction f2 = new ADTFraction(7, 8);
f2.display();
}
}
class ADTFraction {
private int n; //numerator
private int d; //denomenator
//---------------------------------------------------
public ADTFraction() {//default constructor
this.n = 0;
this.d = 1;
}
//---------------------------------------------------
public ADTFraction(int a, int b) {//parameter constructor
if (b != 0) {
this.d = b;
this.n = a;
} else {
this.n = 0;
this.d = 1;
System.out.println("Denomenator cannot be Zero");
}
}
//---------------------------------------------------
public void set(int a, int b) {//set numerator and denomenator
if (b != 0) {
this.d = b;
this.n = a;
} else {
this.n = 0;
this.d = 1;
System.out.println("Denomenator cannot be Zero");
}
}
//---------------------------------------------------
public ADTFraction plus(ADTFraction x) {//add two fractions this=3/5 x=7/8
int num, den;
den = this.d * x.d;
num = this.n * x.d + x.n * this.d;
ADTFraction f1 = new ADTFraction(num, den);
return f1;
}
//---------------------------------------------------
public ADTFraction times(int a) {//multiply fraction by a number
int num, den;
den = this.d;
num = this.n * a;
ADTFraction f1 = new ADTFraction(num, den);
return f1;
//return times(new ADTFraction(a,1))
}
//---------------------------------------------------
public ADTFraction times(ADTFraction x) {//multiply two fractions
int num, den;
den = this.d * x.d;
num = this.n * x.n;
ADTFraction f1 = new ADTFraction(num, den);
return f1;
}
//---------------------------------------------------
public ADTFraction reciprocal() {//reciprocal of a fraction
ADTFraction f1 = new ADTFraction(this.d, this.n);
return f1;
}
//---------------------------------------------------
public float value() {//numerical value of a fraction
return (float) this.n / this.d;
}
//---------------------------------------------------
public void display() {//display the fraction in the format n/d
System.out.println(this.n + "/" + this.d);
}
//---------------------------------------------------
}