-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFractionCalculator.java
126 lines (109 loc) · 5.17 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*************************************************************
* Microsoft: DEV277x - Object Oriented Programming in Java
* Module 2 Project
* ***********************
* FRACTION CALCULATOR
* ***********************
* Author: Zaryab Muhammad Akram
* 2/2/2018
* NNNNNNNNNNNNN Fraction Calculator Class NNNNNNNNNNNNNNNN
*************************************************************/
import java.util.Scanner;
public class FractionCalculator {
public static final Scanner input = new Scanner(System.in);
public static void main(String[] args){
//welcome message
System.out.println("This program is a fraction calculator.");
System.out.println("It will add, subtract, multiply and divide fractions until you type Q to quit.");
System.out.println("Please enter your fractions in the form a/b, where a and b are integers.");
while(true){
System.out.println("----------------------------------------------------------------------------------");
//prompting user to enter in an operation
String operation = getOperation();
//getting two fractions from the user
Fraction input1 = getFraction();
Fraction input2 = getFraction();
Fraction output = new Fraction(1,1); /*declaring a fraction variable for the output*/
//commuting result according to user choice
if (operation.equals("=")) {
boolean result = input1.equals(input2);
System.out.println(input1.toString() + " " + operation + " " + input2.toString() + " = " + result);
continue;
} else 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(); /*convert the output fraction to its lowest form*/
if(output.getDenominator() == 1){
int outInt = output.getNumerator();
System.out.println(input1.toString() + " " + operation + " " + input2.toString() + " = " + outInt);
continue;
}
System.out.println(input1.toString() + " " + operation + " " + input2.toString() + " = " + output);
}
}
/*
* This method prompts the user to enter a valid operation symbol.
* On entering of 'q' or 'Q', the program is terminated.
*/
public static String getOperation(){
System.out.print("Please enter an operation (+, -, /, *, = or Q to quit): ");
String operation = input.nextLine();
while(!operation.equals("+") && !operation.equals("-") && !operation.equals("/") && !operation.equals("*") && !operation.equals("=") && !operation.equalsIgnoreCase("q")){
System.out.print("Invalid input (+, -, /, *, = or Q to quit): ");
operation = input.nextLine();
}
if (operation.equalsIgnoreCase("q")) {
System.exit(0); /*terminating program if the operation input is q or Q*/
}
return operation;
}
/*
* This method checks if the entered fraction is valid or not.
*/
public static boolean validFraction(String input) {
if (input.contains("/")) { /*if input is of the form a/b*/
String[] inputParts = input.split("/"); /* inputParts[0] = numerator, inputParts[1] = denominator */
/*checking if the numerator and denominators are integers*/
if (inputParts[0].matches("-?\\d+") && inputParts[1].matches("-?\\d+")) {
if (Integer.parseInt(inputParts[1]) > 0) {
return true;
} else {
return false; /*negative denominator*/
}
} else {
return false; /*Non-integer numerator or denominator*/
}
} else {
if (input.matches("-?\\d+")) { /*if input in an integer*/
return true;
} else { /*Non integer input*/
return false;
}
}
}
/*
* This method prompts the user to input a fraction(string) of the form a/b and converts it to integer.
*/
public static Fraction getFraction(){
System.out.print("Please enter a fraction (a/b) or integer (a): ");
String inputData = input.nextLine();
while(!validFraction(inputData)){
System.out.print("Invalid fraction. Please enter a fraction (a/b) or (a), where a and b are integers and b is not zero: ");
inputData = input.nextLine();
}
if(inputData.contains("/")){
String[] inputParts = inputData.split("/");
int numInput = Integer.parseInt(inputParts[0]);
int denInput = Integer.parseInt(inputParts[1]);
return new Fraction(numInput, denInput);
} else{
return new Fraction(Integer.parseInt(inputData), 1);
}
}
}