Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fracCalc;
- import java.util.Scanner;
- public class FracCalc {
- public static void main(String[] args)
- {
- // TODO: Read the input from the user and call produceAnswer with an equation
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- produceAnswer(input);
- }
- // ** IMPORTANT ** DO NOT DELETE THIS FUNCTION. This function will be used to test your code
- // This function takes a String 'input' and produces the result
- //
- // input is a fraction string that needs to be evaluated. For your program, this will be the user input.
- // e.g. input ==> "1/2 + 3/4"
- //
- // The function should return the result of the fraction after it has been calculated
- // e.g. return ==> "1_1/4"
- public static String produceAnswer(String input)
- {
- int num1,num2,den1,den2,whole1,whole2;
- String completed = "";
- String[] splitup = input.split(" ");
- String first = splitup[0], second = splitup[1], third = splitup[2];
- System.out.println(third);
- if(first.indexOf("/") != -1 && third.indexOf("/") != -1) { // if both have fractions in them
- String[] firstFrac = splitup[0].split("/");
- String[] secondFrac = splitup[2].split("/");
- den1 = Integer.parseInt(firstFrac[1]);
- den2 = Integer.parseInt(secondFrac[1]);
- if(firstFrac[0].indexOf("_") != -1) {
- String[] wholeNumber1 = firstFrac[0].split("_");
- num1 = Integer.parseInt(wholeNumber1[1]);
- whole1 = Integer.parseInt(wholeNumber1[0]);
- }
- else {
- num1 = Integer.parseInt(firstFrac[0]);
- whole1 = 0;
- }
- if(secondFrac[0].indexOf("_") != -1) {
- String[] wholeNumberAndNumerator = secondFrac[0].split("_");
- num2 = Integer.parseInt(wholeNumberAndNumerator[1]);
- whole2 = Integer.parseInt(wholeNumberAndNumerator[0]);
- }
- else {
- num2 = Integer.parseInt(secondFrac[0]);
- whole2 = 0;
- }
- }
- else if(first.indexOf("/") == -1 && third.indexOf("/") != -1) {// if the second fraction has a fraction in it but the first doesnt
- whole1 = Integer.parseInt(first);
- num1 = 0;
- den1 = 1;
- String[] secondFrac = splitup[2].split("/");
- den2 = Integer.parseInt(secondFrac[1]);
- if(secondFrac[0].indexOf("_") != -1) {
- String[] wholeNumber1 = secondFrac[0].split("_");
- num1 = Integer.parseInt(wholeNumber1[1]);
- whole1 = Integer.parseInt(wholeNumber1[0]);
- }
- else {
- num1 = Integer.parseInt(secondFrac[0]);
- whole1 = 0;
- }
- }else if(first.indexOf("/") != -1 && third.indexOf("/") == -1) { // if the first fraction has a fraction in it but the second doesnt
- whole2 = Integer.parseInt(third);
- num2 = 0;
- den2 = 1;
- String[] firstFrac = splitup[0].split("/");
- den2 = Integer.parseInt(firstFrac[1]);
- if(firstFrac[0].indexOf("_") != -1) {
- String[] wholeNumber1 = firstFrac[0].split("_");
- num1 = Integer.parseInt(wholeNumber1[1]);
- whole1 = Integer.parseInt(wholeNumber1[0]);
- }
- else {
- num1 = Integer.parseInt(firstFrac[0]);
- whole1 = 0;
- }
- }
- System.out.println("whole:" + whole2 + " numerator:" + num1 + " denominator:" + den1);
- return ("whole:" + whole2 + " numerator:" + num2 + " denominator:" + den2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement