-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractionCalculator.java
87 lines (76 loc) · 3.22 KB
/
FractionCalculator.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
import java.util.Scanner;
public class FractionCalculator {
public static final Scanner input = new Scanner(System.in);
public static void main(String[] args){
while(true){
Fraction input1 = getFraction();
Fraction input2 = getFraction();
String operation = getOperation(); //enter operation
Fraction output = new Fraction(1,1); //declare variable
if (operation.equals("-")) {
output = input1.subtract(input2);
} else if (operation.equals("*")) {
output = input1.multiply(input2);
} else if (operation.equals("/")) {
output = input1.divide(input2);
} else {
output = input1.add(input2);
}
output.toLowestTerms(); //lower fraction
if(output.getDenom() == 1){
int outInt = output.getNumer();
System.out.println(input1.toString() + " " + operation + " " + input2.toString() + " = " + outInt);
continue;
}
System.out.println(input1.toString() + " " + operation + " " + input2.toString() + " = " + output);
}
}
public static String getOperation(){
System.out.print("Please enter an operation +, -, /, *, Q to quit: ");
String operation = input.nextLine();
while(!operation.equals("+") && !operation.equals("-") && !operation.equals("/") && !operation.equals("*") && !operation.equalsIgnoreCase("q")){
System.out.print("Invalid input +, -, /, *, Q to quit: ");
operation = input.nextLine();
}
if (operation.equalsIgnoreCase("q")) {
System.exit(0); //exit through q or Q
}
return operation;
}
public static boolean validFraction(String input) {
if (input.contains("/")) {
String[] inputParts = input.split("/"); // inputParts[0] = num, inputParts[1] = denom
if (inputParts[0].matches("-?\\d+") && inputParts[1].matches("-?\\d+")) { //check 4 integer
if (Integer.parseInt(inputParts[1]) > 0) {
return true;
} else {
return false; //negative denominator
}
} else {
return false;
}
} else {
if (input.matches("-?\\d+")) {
return true;
} else { //non integer
return false;
}
}
}
public static Fraction getFraction(){
System.out.print("Enter a/b fraction or integer a: ");
String inputData = input.nextLine();
while(!validFraction(inputData)){
System.out.print("Invalid fraction(-s)");
inputData = input.nextLine();
}
if(inputData.contains("/")){
String[] inputPart = inputData.split("/"); //convert to integer
int numInput = Integer.parseInt(inputPart[0]);
int denInput = Integer.parseInt(inputPart[1]);
return new Fraction(numInput, denInput);
} else{
return new Fraction(Integer.parseInt(inputData), 1);
}
}
}