Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program that prompts (using scanner) the user for two fractions, which

ID: 3628677 • Letter: C

Question

Create a program that prompts (using scanner) the user for two fractions, which are then combined using addition, subtraction, multiplication, and division. Here's what the user will see on the screen:

This program performs arithmetic operations on two fractions.

Enter first fraction: 4/5
Enter second fraction: 3/4

Sum: 31/20
Difference: 1/20
Product: 3/5
Quotient: 16/15

You can assume that the user always enters two integers separated by a slash (/). However, the user may enter any number of spaces before and after each integer. Hint: Use the indexOf, substring, and trim methods from the String class

Explanation / Answer

please rate - thanks

import java.util.*;
import java.lang.*;
public class fractions
{ public static void main(String[] args)
    {Scanner in=new Scanner(System.in);
     int num1,num2,den1,den2,num3=0,den3=0,n=0,m;
    String input,num;
     System.out.print("Enter the first fraction:");
        input=in.next();
        n=input.indexOf('/');
        num=(input.substring(0,n)).trim();
        num1=Integer.parseInt(num);
        m=input.length();
        num=(input.substring(n+1,m)).trim();
        den1=Integer.parseInt(num);
        System.out.print("Enter the second fraction:");
        input=in.next();
        n=input.indexOf('/');
        num=(input.substring(0,n)).trim();
        num2=Integer.parseInt(num);
        m=input.length();
        num=(input.substring(n+1,m)).trim();
        den2=Integer.parseInt(num);
        num3=num1*den2+num2*den1;
        den3=den1*den2;
        print(num3,den3,"Sum");
        num3=num1*den2-num2*den1;
        den3=den1*den2;
        print(num3,den3,"Difference");
        num3=num1*num2;
        den3=den1*den2;
            print(num3,den3,"Product");
         num3=num1*den2;
            den3=den1*num2;
            print(num3,den3,"Quotient");

}
public static int reduce(int num1,int num2)
{num1=Math.abs(num1);
num2=Math.abs(num2);
while (num1 != num2)
     {
          if (num1 > num2)
               num1 -= num2;
          else
               num2 -= num1;
     }
return num1;
}
public static void print(int num3,int den3, String mess)
{ int factor,n;
System.out.print(mess+": ");
factor=reduce(num3,den3);
    num3/=factor;
    den3/=factor;
    if(num3<0&&den3<0)
    {num3=num3*-1;
    den3=den3*-1;
    }
    System.out.println(num3+"/"+den3);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote