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

-3. (20 points) write a program that reads and stores the numerators and denomin

ID: 3881940 • Letter: #

Question

-3. (20 points) write a program that reads and stores the numerators and denominators of two fractions as integer values. For example, if the numbers 1 and 4 are entered for the first fraction the fraction is ¼. The program should print the product of the two fractions as a fraction and as a decimal value. For example, ¼ * ½-1/8 or 0.125. (20 points) Write a program that reads a number of seconds between 0 and 18,000 (5 hours) and displays the hours, minutes, and seconds equivalent. (20 points) Redo Program 3-but this time compute the sum of the two fractions. 4. 5.

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* Below class will add and multiply two fractions
*
*/
public class Fraction
{
   /*
   *   Float variables to store numerators and denominators of two fractions
   */
   private static float numerator1 = 0 ;
   private static float numerator2 = 0 ;
   private static float denominator1 = 0 ;
   private static float denominator2 = 0 ;
  
   /*
   * Result to store multiplication and sum of two fraction.
   */
   private static float result = 0.0f ;
  
   /*
   * Read the numerator and denominator from command line
   */
   private static void readFraction()
   {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
      
       try
       {
           // Enter numerator of first fraction
           System.out.println("Enter numerator for first fraction");
           numerator1 = Integer.parseInt(br.readLine()) ;
          
           do
           {
               //Enter denominator of first fraction. It cannot be zero. zero will lead to exception
               System.out.println("Enter denominator for first fraction");
               denominator1 = Integer.parseInt(br.readLine()) ;
                  
           }while(denominator1 == 0) ;

           // Enter numerator of second fraction
           System.out.println("Enter numerator for fraction 2");
           numerator2 = Integer.parseInt(br.readLine()) ;
          
           do
           {
               //Enter denominator of second fraction. It cannot be zero. zero will lead to exception
               System.out.println("Enter denominator for fraction 2");
               denominator2 = Integer.parseInt(br.readLine()) ;
                  
           }while(denominator2 == 0) ;

           /*
           * Calculate the multiplication of fractions
           */
           calculateMultiplication() ;
          
           /*
           * Calculate the sum of fractions.
           */
           calculateSum() ;
       }
       catch (NumberFormatException e)
       {
           System.out.println("Please enter only number");
          
           e.printStackTrace();
       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
   }
  
   /*  
   *    Calculate and print the multiplication of two fractions
   */
   private static void calculateMultiplication()
   {
       result = (numerator1/denominator1)*(numerator2/denominator2) ;
      
       System.out.println("Multiplication of two number is " + result + " ");
   }

   /*  
   *    Calculate and print the sum of two fractions
   */
   private static void calculateSum()
   {
       result = (numerator1/denominator1)+(numerator2/denominator2) ;
      
       System.out.println("Sum of two number is " + result + " ");
   }

   /**
   *
   * Starting point of java program
   */
   public static void main(String[] args)
   {
       /*
       * Read fractions from user
       */
       readFraction() ;
   }
}