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

How to print returned methods in JAVA. I have written a code, but I need the out

ID: 3766969 • Letter: H

Question

How to print returned methods in JAVA. I have written a code, but I need the out put to look like this:-

The Code I have wriiten so far is this :-

import java.util.Scanner;

public class Fraction {

  
   /* Fraction methods for adding, subtracting, multiplying, and dividing rational number
   * also will have to compute greatest common denominator for use with the add/subtract
   */
   public static void main(String[] args){
      
      
           Scanner input = new Scanner(System.in);
           int[] num = new int[2];
           int[] den= new int[2];
  
           System.out.println("==F1==");
          
           System.out.print("Enter the numerator : ");
           num[0] = input.nextInt();
          
           System.out.print("Enter the denominator : ");
           den[1] = input.nextInt();
           if(den[1] == 0){
               System.out.println("Wrong denominator");
               System.exit(0);
           }
          
           System.out.println("==F2==");
          
           System.out.print("Enter the numerator : ");
           num[0] = input.nextInt();
          
           System.out.print("Enter the denominator : ");
           den[1] = input.nextInt();
           if(den[1] == 0){
               System.out.println("Wrong denominator");
               System.exit(0);
           }
   }
     
      private int num; //numerator
      private int den; //denominator
     
      public Fraction(int n, int d) // constructor that requires 2 digits of input
      {
          // assign num = n & den = d as long as d!= 0
          if(d != 0)
          {
              num = n;
              den = d;
          }
          else
              System.exit(0);       
      }
      private static int gcd(int x, int y)
      {
          /* gcd() method finds the greatest common divisor of
          * the 2 int variables passed and returns that value
          *
          */
          int mod; // hold a value temporarily to allow switch
          if(x < y) // always use the larger value as the divisor
          {
          mod = x;
          x = y;
          y = mod;      
          }
          int r = x % y; // r holds the remainder of modulus division
           while (r != 0) // while modulus division does not result in zero
           {
           x = y;
           y = r;
           r = x % y;
           }
           return y;
         
      }
      private Fraction reduce(int n, int d)
      {
          int gcdNum = gcd(n,d);
         
          d = d / gcdNum; // reduce the denominator using the gcd foun in gcd method
          n = n / gcdNum; // reduce the numerator using the gcd foun in gcd method
          return new Fraction(n,d);   // return the new fraction object in lowest form
      }
      public Fraction add(Fraction b)
      {
          int num1 = (this.num * b.den) + (b.num * this.den); // cross multily and add
          int   num2 = this.den * b.den; // multiply the denominators to make them equivlent
         
          return reduce(num1,num2);// calls reduce() method and returns a new Fraction object
          
      }
      public Fraction subtract(Fraction b)
      {
          int num1 = (this.num * b.den) - (b.num * this.den);// cross multiply and subtact
          int num2 = this.den * b.den;
     
          return reduce(num1,num2);// sends to reduce method
      }
      public Fraction multiply(Fraction b)
      {
          int num1 = this.num * b.num; // multiplys straight accross
          int   num2 = this.den * b.den;
         
          return reduce(num1,num2);// sends to reduce method and returns the reduced fraction to the toString() method
      }
      public Fraction divide(Fraction b)
      {       
          int num1 = this.num * b.den; //multiplys the inverse of 2nd fraction object to divide
          int num2 = this.den * b.num;
         
          return reduce(num1, num2);// sends to reduce method
      }
      public String toString() // *** convert to a mixed fraction for output only
      {     
          if(num > den && den > 1)   //if true will show fraction object and mixed number                                 
              return (num + "/" + den + " or " + (num/den) + " " + (num % den) + "/" + den);      
          else
              return(num + "/" + den); //will not try to convert fraction object to mixed number
         
      }
   }

You will also have to write a program to test your fraction class. The following output is an example run: Enter numerator:1 Enter denominator: 2 Enter numerator:3 Enter denominator: 5 F1: 1/2 (0.500) F2: 3/5 (0.600) F1+F1: 1 F2+F2: 6/5 (1.200) F1+F2: 11/10 (1.100) F2+F1: 11/10 (1.100) F2-F2: 0 F1-F2: -1/10 (-0.100) F2-F1: 1/10 (0.100) F1"F1: 1/4 (0.250) F2 F2: 9/25 (0.360) F1 F2: 3/10 (0.300) F2 F1: 3/10 (0.300) F1/F1: 1 F2/F2: 1 F1/F2: 5/6 (0.833) F2/F1 6/5 (1.200) F1 F1-F2:7/20 (0.350) The program should use the Scanner constructor, the plus/minus/times/divides methods (as opposed to directly computing the outputs), and implicitly the toString method.

Explanation / Answer

import java.util.Scanner;

public class Fraction {
  
    /* Fraction methods for adding, subtracting, multiplying, and dividing rational number
     * also will have to compute greatest common denominator for use with the add/subtract
     */
    public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            int[] num = new int[2];
            int[] den= new int[2];
  
            System.out.println("==F1==");
          
            System.out.print("Enter the numerator : ");
            num[0] = input.nextInt();
          
            System.out.print("Enter the denominator : ");
            den[0] = input.nextInt();
            if(den[0] == 0){
                System.out.println("Wrong denominator");
                System.exit(0);
            }
          
            System.out.println("==F2==");
          
            System.out.print("Enter the numerator : ");
            num[1] = input.nextInt();
          
            System.out.print("Enter the denominator : ");
            den[1] = input.nextInt();
            if(den[1] == 0){
                System.out.println("Wrong denominator");
                System.exit(0);
            }
            System.out.println();
            Fraction f1 = new Fraction(num[0], den[0]);
            Fraction f2 = new Fraction(num[1], den[1]);
            System.out.println("F1: " + f1);
            System.out.println("F2: " + f2);
            System.out.println("F1+F1: " + f1.add(f1));
            System.out.println("F2+F2: " + f2.add(f2));
            System.out.println("F1+F2: " + f1.add(f2));
            System.out.println("F2+F1: " + f2.add(f1));
            System.out.println("F1-F1: " + f1.subtract(f1));
            System.out.println("F2-F2: " + f2.subtract(f2));
            System.out.println("F1-F2: " + f1.subtract(f2));
            System.out.println("F2-F1: " + f2.subtract(f1));
            System.out.println("F1*F1: " + f1.multiply(f1));
            System.out.println("F2*F2: " + f2.multiply(f2));
            System.out.println("F1*F2: " + f1.multiply(f2));
            System.out.println("F2*F1: " + f2.multiply(f1));
            System.out.println("F1/F1: " + f1.divide(f1));
            System.out.println("F2/F2: " + f2.divide(f2));
            System.out.println("F1/F2: " + f1.divide(f2));
            System.out.println("F2/F1: " + f2.divide(f1));
            System.out.println("F1*F1-F2: " + (f1.multiply(f1)).subtract(f2));

    }
   
         private int num; //numerator
         private int den; //denominator
       
         public Fraction(int n, int d) // constructor that requires 2 digits of input
         {
             // assign num = n & den = d as long as d!= 0
             if(d != 0)
             {
                 num = n;
                 den = d;
             }
             else
                 System.exit(0);       
         }
         private static int gcd(int x, int y)
         {
             /* gcd() method finds the greatest common divisor of
              * the 2 int variables passed and returns that value
              *
              */
             int mod; // hold a value temporarily to allow switch
             if(x > y) // always use the larger value as the divisor
             {
                mod = x;
                x = y;
                y = mod;      
             }
             int r = x % y; // r holds the remainder of modulus division
            while (r != 0) // while modulus division does not result in zero
            {
              x = y;
              y = r;
              r = x % y;
            }
            return y;
           
         }
         private Fraction reduce(int n, int d)
         {
             int gcdNum = gcd(n,d);
           
             d = d / gcdNum; // reduce the denominator using the gcd foun in gcd method
             n = n / gcdNum; // reduce the numerator using the gcd foun in gcd method
             return new Fraction(n,d);    // return the new fraction object in lowest form
         }
         public Fraction add(Fraction b)
         {
             int num1 = (this.num * b.den) + (b.num * this.den); // cross multily and add
             int    num2 = this.den * b.den; // multiply the denominators to make them equivlent
           
             return reduce(num1,num2);// calls reduce() method and returns a new Fraction object
          
         }
         public Fraction subtract(Fraction b)
         {
             int num1 = (this.num * b.den) - (b.num * this.den);// cross multiply and subtact
             int num2 = this.den * b.den;
       
             return reduce(num1,num2);// sends to reduce method
         }
         public Fraction multiply(Fraction b)
         {
             int num1 = this.num * b.num; // multiplys straight accross
             int    num2 = this.den * b.den;
           
             return reduce(num1,num2);// sends to reduce method and returns the reduced fraction to the toString() method
         }
         public Fraction divide(Fraction b)
         {       
             int num1 = this.num * b.den; //multiplys the inverse of 2nd fraction object to divide
             int num2 = this.den * b.num;
           
             return reduce(num1, num2);// sends to reduce method
         }
         public String toString() // *** convert to a mixed fraction for output only
         {     
             if(num > den && den > 1)     //if true will show fraction object and mixed number                               
                 return (num + "/" + den + " or " + (num/den) + " " + (num % den) + "/" + den);      
             else
                 return(num + "/" + den); //will not try to convert fraction object to mixed number
           
         }
     }

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