Define a class for a type called Fraction This class is used
Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set and get the numerator and the denominator. Also include a function that returns the values of the numerator divided by the denominator as a double. Also include a member function that outputs the value of the fraction reduced to the lowest terms. This will require finding the greatest common divisor for the numerator and denominator, and dividing by that number. Show the output of your code using the following 2 pairs of values: Test 1: numerator = 15, denominator = 25; Test 2: numerator = 27, denominator = 6.
Solution
Fraction.java
 import java.util.Scanner;
public class Fraction
 {
 private int numerator;
 private int denominator;
 public Fraction()
 {
 numerator = 0;
 denominator=1;
 }
 public Fraction(int n, int d)
 { int g=gcd(n,d);
 //complete this
 numerator = n;
 denominator=d;
 }
 
 public int getNumerator() {
 return numerator;
 }
 
 public void setNumerator(int n) {
 int d=denominator;
 int g=gcd(n,d);
 //complete this
 this.numerator = n;
 }
 
 public int getDenominator() {
 return denominator;
 }
   
 public void setDenominator(int d) {
 int n=numerator;
 int g=gcd(n,d);
 //complete this
 denominator = d;
 }
 public Fraction add(Fraction g)
 {
 int a=this.numerator;
 int b=this.denominator;
 int c=g.numerator;
 int d=g.denominator;
 Fraction v=new Fraction(a*d+b*c,b*d);
 return v;
 }
 public Fraction subtract(Fraction g)
 {
 int a=this.numerator;
 int b=this.denominator;
 int c=g.numerator;
 int d=g.denominator;
 Fraction v=new Fraction(a*d-b*c,b*d);
 return v;
 }
 public Fraction multiply(Fraction g)
 {
 int a=this.numerator;
 int b=this.denominator;
 int c=g.numerator;
 int d=g.denominator;
 Fraction v=new Fraction(a*c,b*d);
 return v;
 }
 public Fraction divide(Fraction g)
 {
 int a=this.numerator;
 int b=this.denominator;
 int c=g.numerator;
 int d=g.denominator;
 Fraction v=new Fraction(a*d,b*c);
 return v;
 }
 public String toString()
 {
 return numerator + \"/\" + denominator;
 }
   
 private int gcd(int int1, int int2)
 {
 //complete this
    return int2 == 0 ? int1 : gcd(int2, int1 % int2);
 }
 
public static void main(String[] args) {
 
   
 Scanner keyboard = new Scanner (System.in);
 //get the data for the next fraction and construct it
 int n1=keyboard.nextInt();
 int d1=keyboard.nextInt();
 Fraction one=new Fraction (n1,d1);
 //get the data for the next fraction and construct it
 n1=keyboard.nextInt();
 d1=keyboard.nextInt();
 Fraction two=new Fraction (n1,d1);
 //test methods for add, subtract, multiply, divide
 System.out.println(one + \" + \" + two + \" = \" + one.add(two));
 System.out.println(one + \" - \" + two + \" = \" + one.subtract(two));
 System.out.println(one + \" * \" + two + \" = \" + one.multiply(two));
 System.out.println(one + \" divided by \" + two + \" = \" + one.divide(two));
   
 }
 }
Output:
15
 25
 27
 6
 15/25 + 27/6 = 765/150
 15/25 - 27/6 = -585/150
 15/25 * 27/6 = 405/150
 15/25 divided by 27/6 = 90/675



