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

Rational fractions are of the form a / b, wherea and b are integers and b 6¼ 0.

ID: 3661894 • Letter: R

Question

Rational fractions are of the form a / b, wherea and b are integers and b 6¼ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose that a / b and c /d are fractions. Arithmetic operationson fractions aredefined by the following rules: a / b + c / d ¼ (ad + bc)/bd a / b – c / d ¼ (ad – bc)/bd a / b c / d ¼ ac / bd (a / b)/( c / d) ¼ ad / bc, where c / d 6¼ 0 Fractions are compared as follows: a / b op c/ d if ad op bc, where op is any of the relational operations. For example, a / b < c / d if ad < bc. Design the class Fraction that can be used to manipulate fractions in a program. Among others, the class Fraction must include methods to add, subtract, multiply, and divide fractions. When you add, subtract, multiply, or divide fractions, your answer need not be in the lowest terms. Also, override the methodtoStringsothatthefractionscanbeoutputusingtheoutputstatement. Write a Java program that, using the class Fraction, performs operations on fractions.

Explanation / Answer

The following program is used to perform operations like addition,subtraction,multiplication and division on the ration fractions with the help of classes.The two fractions are input by the user and appropriate functions are called to perform each operation depending on the choice which the user choses to from the list of of options.After the function is executed,the result is given in the form of a fraction itself.

import java.io.*;
   import java.util.Scanner;
class calculatefraction{
class Fraction{
                  private int numer; // numer=numerator,denom=denominator of the fractions
                  private int denom;
                  Fraction()

              {numer=0;

              denom=1;}
                  Fraction(int p,int q)

                 {numer=p;

                  denom=q;}

     }

          private static Fraction f1;

      private static Fraction f2;


   public static void main(String[] args){
       int choice;
       Scanner sc=new Scanner(System.in);
       calculatefraction cf=new calculatefraction();
       Fraction f;
       printMenu();
           System.out.print("Choose one of following:");
      choice=sc.nextInt();
   switch(choice){
             case 1: cf.getdata(); // to get the fractions /input them
                           f=cf.addFraction(f1,f2);
                           System.out.format(" Result:%d/%d ",f.numer,f.denom);
                           break;
               case 2: cf.getdata();
                            f=cf.subtractFraction(f1,f2);
                            System.out.format(" Result:%d/%d ",f.numer,f.denom);
                            break;
               case 3: cf.getdata();
                             f=cf.multiplyFraction(f1,f2);
                             System.out.format(" Result:%d/%d ",f.numer,f.denom);
                             break;
               case 4: cf.getdata();
                             f=cf.divideFraction(f1,f2);
                             System.out.format(" Result:%d/%d ",f.numer,f.denom);
                             break;
                   default: System.out.println("Invalid choice!");
   }
   }

   public Fraction addFraction(Fraction f1,Fraction f2){

// addition of 2 fractions
                Fraction result=new Fraction();
                resutl.numer=(f1.numer*f2.denom)+(f1.denom*f2.numer);
                result.denom=f1.denom*f2.denom;
                return result;
}
   public Fraction subtractFraction(Fraction f1,Fraction f2){

// subtraction of 2 fractions
                 Fraction result=new Fraction();
                 result.numer=(f1.numer*f2.denom)-(f1.denom*f2.numer);
                 result.denom=f1.denom*f2.denom;
                 return result;
}
  public Fraction multiplyFraction(Fraction f1,Fraction f2){

//multiplication of 2 fractions
                 Fraction result=new Fraction();
                 resul.numer=f1.numer*f2.numer;
                 resul.denom=f1.denom*f2.denom;
                 return result;
}
  public Fraction divideFraction(Fraction f1,Fraction f2){

//division of 2 fractions
                 Fraction result=new Fraction();
                 resul.numer=f1.numer*f2.denom;
                 resul.denom=f1.denom*f2.numer;
                 return result;
}
public static void printMenu(){
                 System.out.println("Calculations on the fractions");
                 System.out.println("-------------------------------");
                 System.out.println("1. Adding fractions");
                 System.out.println("2. Subtracting fractions");
                 System.out.println("3. Multiplying fractions");
                  System.out.println("4. Dividing fractions");
     }
public void getdata(){
               String[] mytext=new String[2];
String[] newtext=new String[2];
                 int numer,denom;
                 f1=new Fraction();
                 f2=new Fraction();
                 int i;
               
                try{
                     for(i=0;i<=1;i++){
                          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                      System.out.format("Enter the fraction for performing calculations %d (e.g. 2/3):",i+1);
                         ftext[i]=br.readLine();

                  }
               newtext=mytext[0].split("/");
               f1.numer=Integer.parseInt(sptext[0]);
               f1.denom=Integer.parseInt(sptext[1]);

              newtext=mytext[1].split("/");
              f2.numer=Integer.parseInt(sptext[0]);
              f2.denom=Integer.parseInt(sptext[1]);
              if(f1.denom==0 || f2.denom==0) {

System.out.println("A denominator of the fraction can't be zero/wrong fraction ");

System.exit(100);}

                }catch(IOException e){System.out.println("Error in reading user input...");}
   }
  }

Output:

                 Calculations on the fractions
                   -------------------------------
                 1. Adding fractions
                 2. Subtracting fractions
                 3. Multiplying fractions
                 4. Dividing fractions

             Choose one of the following:1

               Enter the fraction for performing calculations 1:3/2

                Enter the fraction for performing calculations 2:2/3

     Result: 13/6

option 2:

Result:5/6

Option 3:

Result:6/6

Option 4

Result: 9/4