• Java problem using 1/2 and 3/4 for the variables

    HeatherKort Member

    Here is the code I was given

    
    import java.util.Scanner;
    
    class Fraction
    {
      public static void main (String[] args) {
          String div; //decalring a variable for the string
          int a, b, c, d;
          Scanner scan = new Scanner(System.in); //creating an object to use in the program below
         
          System.out.print("Type a rational number (a / b): ");
          a = scan.nextInt();
          div = scan.next("/");
          b = scan.nextInt();
         
          System.out.print("Type a another rational number (c / d): ");
          c = scan.nextInt();
          div = scan.next("/");
          d = scan.nextInt();
    
          System.out.println("The sum in fractions is ");
          System.out.println(The input here is suppose to hold the same values for the variables previously used above- not sure how to do this);
    
    
          System.out.println("The sum in decimals is ");
          System.out.println(Again based on input from the user, this is suppose to be used with a float casting to provide a precise sum);     
       
      }
    
    }
    

    The output of the code is suppose to look like this, when using 1/2 and 3/4 for the variables. Any help would be greatly appreciated.

    
    Enter a fraction (a / b): 1 / 2
    Enter another fraction (a / b): 3 / 4
    
    The sum in fractions is
    1/2 + 3/4 = 10/8
    
    The sum in decimals is
    0.5 + 0.75 = 1.25
    
  • Adelaid Member

    Where you want to print out the variables in the last println statements mixed with integers but mention you’re not sure how to do it, you use string concatenation. From the example i used below:

    System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + calcFraction(a, b, c, d));
    

    To calculate the sum in fractions you need to find the LCD (lowest common denominator) and the GCD is then used to calculate the simplified fraction. With some googling I found already made methods for these. The code still has exceptions to be handled but works with the example cases, if you need any clarification just let me know. (Second year CS student)

    import java.text.DecimalFormat;
    import java.util.Scanner;
    class Fraction {
    static DecimalFormat df = new DecimalFormat("###,###,###.###");
    
    public static void main(String[] args) {
    
    String div; //decalring a variable for the string
    int a, b, c, d;
    Scanner scan = new Scanner(System.in); //creating an object to use in the program below
    
    System.out.print("Type a rational number (a / b): ");
    a = scan.nextInt();
    div = scan.next("/");
    b = scan.nextInt();
    
    System.out.print("Type a another rational number (c / d): ");
    c = scan.nextInt();
    div = scan.next("/");
    d = scan.nextInt();
    
    System.out.println("The sum in fractions is");
    System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + calcFraction(a, b, c, d));
    
    
    System.out.println("The sum in decimals is ");
    System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + df.format(calcDecimal(a, b, c, d)));
    }
    
    private static int calcGCD(int a, int b) {
    if (b == 0) {
    return a;
    }
    return calcGCD(b, a % b);
    }
    
    private static int calcLCM(int x1, int x2) {
    if (x1 <= 0 || x2 <= 0) {
    throw new IllegalArgumentException("Cannot compute the least "
    + "common multiple of two "
    + "numbers if one, at least,"
    + "is negative.");
    }
    int max, min;
    if (x1 > x2) {
    max = x1;
    min = x2;
    } else {
    max = x2;
    min = x1;
    }
    for (int i = 1; i <= min; i++) {
    if ((max * i) % min == 0) {
    return i * max;
    }
    }
    throw new Error("Cannot find the least common multiple of numbers "
    + x1 + " and " + x2);
    }
    
    private static String calcFraction(int a, int b, int c, int d) {
    String ans = null;
    
    int lcm = calcLCM(b, d);
    int timesToMultiplyAB = lcm / b;
    a *= timesToMultiplyAB;
    
    int timesToMultiplyCD = lcm / d;
    c *= timesToMultiplyCD;
    
    int numerator = a + c;
    int denominator = lcm;
    
    int gcd = calcGCD(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    
    ans = numerator + "/" + denominator;
    return ans;
    }
    private static double calcDecimal(double a, double b, double c, double d) {
    double ans;
    double left = a / b, right = c / d;
    ans = left + right;
    return ans;
    }
    }
    
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish