View the step-by-step solution to:

Question

Hello, I am currently working on a Fraction Calculator project for CSA.


One part of the code's

objective is if the user inputs a mixed number, the code should convert it to an improper fraction- I got this part done, but this code also has to deal with negative integers. It's an obvious that two negatives cancel out a positive, but my code doesn't recognize that/ I'm not sure how to solve this issue :/ For example, if the user inputs "-1/-7", the code should output positive 1/7, but it just executes the negative fraction. I will have my code attached below.


import java.util.*;

public class FracCalc{

  public static Scanner console = new Scanner(System.in);

  public static void main(String[] args){  

    String input = "";

    while(!input.equalsIgnoreCase("quit")) { // asks user for input

      System.out.println("Enter:");

      input = console.nextLine();

      System.out.println(processCommand(input));

      System.out.println();

    }//while(!input.equalsIgnoreCase("quit")) will continue to be executed if user puts in 'hi'

  }


  public static String processCommand(String input){

    if(!UnitTestRunner.processCommand(input)){

      if(input.equalsIgnoreCase("help")){// user can call for help

        helpMethod();

      }else if(input.equalsIgnoreCase("quit")){

        System.out.println("Goodbye! See you next time.");

      }else if (input.contains("+") || input.contains("-") || input.contains("*") || input.contains("/")) {//if the input has any of

        //these operators, then the code will call processExpression

        return processExpression(input);

      }

    }

    return input; //if the code does not have any of the operators listed above, then the code will just return the input given by the user.

  }


  public static void helpMethod(){//this is w hat prints when user asks for help

    System.out.println("Oh no! It looks like you need help.");

    System.out.println("That's alright, you'll be okay. Ask Mr. Binz for some support!");

  }


  public static String processExpression(String input){//this method will decompose the expressions given by the user

    String frac1 = input.substring(0, input.indexOf(" "));

    input = input.substring(input.indexOf(" ") + 1);

    String op = input.substring(0, input.indexOf(" "));

    String frac2 = input.substring(input.indexOf(" ") + 1);

    frac1 = processFraction(frac1);

    frac2 = processFraction(frac2);

    return calculate(frac1, frac2, op);

  }


  public static String processFraction(String frac){//processFraction examines the input given by the user and determines whether it's positive or negative.

    if (!frac.contains("/")){//whole num

      return frac + "/1";

    }else{

      if(frac.contains("_")){ //if given input is a value that contains a '_', it is a mixed number.

        if(frac.contains("-")){ // 4_1/-2 -7/-2

          int wholeNum = Integer.valueOf(frac.substring(0, frac.indexOf("_")));

          int num = Integer.valueOf(frac.substring(frac.indexOf("_") + 1, frac.indexOf("/")));

          int denom = Integer.valueOf(frac.substring(frac.indexOf("/") + 1));

          num = wholeNum * denom + num;

          //returns a both pos fraction

          return num + "/" + denom; //if the fraction contains a minus symbol, then it is negative.

        }else{//if the fraction does not contain a minus symbol, then just return the fraction as is.

          int wholeNum = Integer.valueOf(frac.substring(0, frac.indexOf("_")));

          int num = Integer.valueOf(frac.substring(frac.indexOf("_") + 1, frac.indexOf("/")));

          int denom = Integer.valueOf(frac.substring(frac.indexOf("/") + 1));

          num = wholeNum * denom + num;

          return num + "/" + denom;

        }

        //SINCE you convert a negtive improper fraction to its rightful negative state

        // ABOVE, just make that its own method so you can reuse that code for 

        // processing normal negative fraction

        //NEED TO add condition that processes non-mixed number fracs

        //return frac;

      }

      return frac;

    }


  }


  public static String calculate(String frac1, String frac2, String op){

    //converting strings to int in order to calculate 

    int num1 = Integer.valueOf(frac1.substring(0, frac1.indexOf("/")));

    int denom1 = Integer.valueOf(frac1.substring(frac1.indexOf("/") + 1));

    int num2 = Integer.valueOf(frac2.substring(0, frac2.indexOf("/")));

    int denom2 = Integer.valueOf(frac2.substring(frac2.indexOf("/") + 1));

    if(op.equals("+")){

      int numOne = num1 * denom2;

      int numTwo = num2 * denom1;

      int num = numOne + numTwo;

      int denom = denom1 * denom2;

      int g = gcd(num, denom);

      return convertToOutput(num/g, denom/g);//the return statement will be divided by the gcd to get the reduced outcome.

    }else{

      if(op.equals("-")){

        int minusNum1 = num1*denom2;

        int minusNum2 = num2*denom1;

        int minusNum = minusNum1 - minusNum2;

        int minusDenom = denom1*denom2;

        int g = gcd(minusNum, minusDenom);

        return convertToOutput(minusNum/g, minusDenom/g);

      }else if(op.equals("*")){

        int multNum = num1*num2;

        int multDenom = denom1*denom2;

        int g = gcd(multNum, multDenom);

        return convertToOutput(multNum/g, multDenom/g);

      }else if(op.equals("/")){

        int divideNum1 = num1 * denom2;

        int divideNum2 = num2 * denom1;

        int g = gcd(divideNum1, divideNum2);

        return convertToOutput(divideNum1/g, divideNum2/g);

      }

    }

    if(frac1.contains("_")){//if the input contains a "_", then it is a mixed number.

      int wholeNum = Integer.valueOf(frac1.substring(1, frac1.indexOf("_")));

      int num = Integer.valueOf(frac1.substring(frac1.indexOf("_") + 1, frac1.indexOf("/")));

      int denom = Integer.valueOf(frac1.substring(frac1.indexOf("/") + 1));

      int improperFrac1 = wholeNum * denom + num / denom; //this line of code converts mixed numbers into

      //improper fractions so they can be operated.

      int improperFrac2 = wholeNum * denom + num / denom;

      int g = gcd(improperFrac1, improperFrac2);

      return convertToOutput(improperFrac1/g , improperFrac2/g);

    }

    return processFraction(num1 + "/" + denom1);//if the input contains no operators, then the code will just return the input.

  }  


  public static int gcd(int a, int b){

    a = Math.abs(a);

    b = Math.abs(b);

    if(b == 0) return a;

    return gcd(b, a%b);

  }


  public static String convertToOutput(int num, int denom){// convertToOutput will convert a improper fraction into a mixed number

    // or a mixed number into an improper fraction.

    int wholeNum = num/denom; //i have to define wholeNum outside of the if else loop or the code will not recognize the wholeNum value.

    if(Math.abs(num) >= Math.abs(denom)){

      if(num % denom == 0){

        return String.valueOf(wholeNum);

      }else{

        int whole = num/denom;

        num = num % denom;

        if(wholeNum < 0 && num < 0){ //if both wholeNum and numerator are less than 0, the new numerator will be the numerator and -1 multiplied.

          num = num * -1;

          return wholeNum + "_" + num + "/" + denom;

        }else{

          return wholeNum + "_" + num + "/" + denom;

        }

      }

    }else if(denom != 1){//if the denominator is not equal to 1, then the code will just return the fraction.

      return num + "/" + denom;

    }else{

      return String.valueOf(num);

    }

  }

}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Let our 24/7 Java Programming tutors help you get unstuck! Ask your first question.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors