-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractionv2.java
72 lines (45 loc) · 1.2 KB
/
Fractionv2.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
import java.util.Scanner;
public class Fractionv2 {
private int numerator, denominator = 1;
public Fractionv2() {}
public Fractionv2(int num) {
numerator = num;
denominator = 1;
}
public Fractionv2(int num, int den) {
if (den != 0) {
numerator = num;
denominator = den;
}
}
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;
Fractionv2 f = new Fractionv2();
for (int i = 0; i < 5; i++) {
System.out.print("Enter your numerator and denominator: ");
a = kb.nextInt();
b = kb.nextInt();
f = new Fractionv2(a, b);
System.out.println("Fraction: " + f.toString() + ", decimal: " + f.toDouble());
System.out.println("Fraction value cubed: " + Math.pow(f.toDouble(), 3));
}
}
}