-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraction.java
57 lines (33 loc) · 938 Bytes
/
Fraction.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
import java.util.Scanner;
public class Fraction {
private int numerator, denominator = 1;
public void setFraction(int num, int den) {
if (den != 0) {
numerator = num;
denominator = den;
}
}
//Not in the assignment, but useful and safe
public int getNumerator() {return numerator;}
public int getDenominator() {return denominator;}
public double toDouble() {
return (double)numerator/denominator;
}
public String toString() {
return numerator + "/" + denominator;
}
}
class FractionTester {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
int a, b;
Fraction f = new Fraction();
for (int i = 0; i < 5; i++) {
System.out.print("Enter your numerator and denominator: ");
a = kb.nextInt();
b = kb.nextInt();
f.setFraction(a, b);
System.out.println("Fraction: " + f.toString() + ", decimal: " + f.toDouble());
}
}
}