Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class example{
- public static int number1;
- public static int number2;
- public static int wholenum1;
- public static int wholenum2;
- public static int division1;
- public static int division2;
- public static int newn; //New;;;;;;;;;;
- public static int newd; //New;;;;;;;;;;
- public static void main(String[] args) { //Main method
- //Scanner scan = new Scanner(System.in);
- System.out.println("Welcome to FracCalc!");
- System.out.println("This version of Frat Calc supports (+, -, * and /)");
- System.out.println("Type quit to quit Frac Calc, and type test to test Frac Calc");
- System.out.println("Bye. Come again!");
- }// Ends main
- public static String produceAnswer2(String input) { //Method2
- boolean multipleoperations = false;
- String[] x = input.split(" "); //Splits the string apart
- String fraction1 = x[0];
- String operator = x[1];
- String fraction2 = x[2];
- String operand1 = parseOperands(fraction1); //Calls the ParseOperands method
- String operand2 = parseOperands(fraction2); //Calls the ParseOperands method
- String[] operand3 = operand1.split(" ");
- int wholenum1 = Integer.parseInt(operand3[1]);
- int numerator1 = Integer.parseInt(operand3[3]);
- int denominator1 = Integer.parseInt(operand3[5]);
- String[] finaloperand2 = operand2.split(" ");
- int wholenum2 = Integer.parseInt(finaloperand2[1]);
- int numerator2 = Integer.parseInt(finaloperand2[3]);
- int denominator2 = Integer.parseInt(finaloperand2[5]);
- String resultingnumbers = ("");
- int result = 0;
- int numeratoranswer = 0;
- int denominatoranswer = 0;
- }
- public static String Addition(String input) {
- if (operator.equals("+")) { //This if statment occurs when the user uses addition
- result = wholenum1 + wholenum2;
- numeratoranswer = (numerator1 * denominator2 + numerator2 * denominator1);
- denominatoranswer = (denominator1 * denominator2);
- numeratoranswer = result * denominatoranswer + numeratoranswer;
- result = 0;
- resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
- }
- if (operator.equals("-")) { //This if statment occurs when the user uses subtraction
- int num = 0;
- int num2 = 0;
- if (wholenum1 < 0) { //If statements below determine the numerators values
- num = wholenum1 * denominator1 - numerator1;
- }
- if (wholenum1 > 0) {
- num = wholenum1 * denominator1 + numerator1;
- }
- if (wholenum2 > 0) {
- num2 = wholenum2 * denominator2 + numerator2;
- }
- if (wholenum2 < 0) {
- num2 = wholenum2 * denominator2 - numerator2;
- }
- if (wholenum1 == 0) {
- num = numerator1;
- }
- if (wholenum2 == 0) {
- num2 = numerator2;
- }
- num *= denominator2;
- num2 *= denominator1;
- numeratoranswer = num - num2;
- denominatoranswer = denominator1 * denominator2;
- result = 0;
- resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
- }
- if (operator.equals("*")) { //This if statment occurs when the user uses multiplication
- numeratoranswer = (wholenum1 * denominator1 + numerator1) * (wholenum2 * denominator2 + numerator2);
- denominatoranswer = (denominator1 * denominator2);
- result = 0;
- resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
- }
- if (operator.equals("/")) { //This if statment occurs when the user uses division
- numerator1 = (wholenum1 * denominator1 + numerator1);
- numerator2 = (wholenum2 * denominator2 + numerator2);
- int temporarydenominator2 = denominator2;
- denominator2 = numerator2;
- numerator2 = temporarydenominator2;
- numeratoranswer = numerator1 * numerator2;
- denominatoranswer = denominator1 * denominator2;
- result = 0;
- resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
- }
- result = numeratoranswer / denominatoranswer;
- numeratoranswer = numeratoranswer % denominatoranswer;
- resultingnumbers = result + ("_") + (Math.abs(numeratoranswer)) + ("/") + (Math.abs(denominatoranswer));
- if (result == 0 && numeratoranswer == 0 && denominatoranswer == 1) {
- resultingnumbers = ("0");
- return resultingnumbers;
- }
- if (numeratoranswer == 0 && denominatoranswer == 1) {
- resultingnumbers = ("") + result;
- return resultingnumbers;
- }
- if (result == 0) {
- resultingnumbers = ("") + numeratoranswer + ("/") + denominatoranswer;
- return resultingnumbers;
- }
- return resultingnumbers;
- }
- public static String parseOperands(String operand2) { //Method 3
- boolean underscore = false;
- boolean fraction = false;
- boolean dividezero = false;
- String wholenum = ("");
- String fractionunparsed = ("");
- String numerator = ("");
- String denominator = ("");
- for (int j = 0; j < operand2.length(); j++) {
- if (operand2.charAt(j) == '_') { //If statement checks characters in String for an underscore
- underscore = true;
- }
- if (operand2.charAt(j) == '/') { //If statement checks characters in String for a dash
- fraction = true;
- }
- }
- if (underscore == true) { //If there's a underscore this will split the string into a fraction and a whole
- String[] components = operand2.split("_");
- wholenum = components[0];
- fractionunparsed = components[1];
- }
- else {
- if (fraction == true) { //If there's no underscore but there's a fraction this will be used
- wholenum = "0";
- fractionunparsed = operand2;
- }
- else { //If there's no underscore and no fraction this will be used
- wholenum = operand2;
- fractionunparsed = ("");
- }
- }
- if (fraction == true) { //If there's no fraction this will take the fraction and split it into the numerator and denominator of the fraction
- String[] fractions = fractionunparsed.split("/");
- numerator = fractions[0];
- denominator = fractions[1];
- }
- else { //If there's no fraction both the numerator and denominator are set to values of 0 and 1
- numerator = "0";
- denominator = "1";
- }
- String operand2parsed = "wholenumber: " + wholenum + " numerator: " + numerator + " denominator: " + denominator;
- if (denominator.equals("0")) {
- dividezero = true;
- return "dividebyzero";
- }
- return operand2parsed; //Returns operand2parsed
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement