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

Create a Java class called Rational for performing arithmetic operations with Ra

ID: 3870071 • Letter: C

Question

Create a Java class called Rational for performing arithmetic operations with Rationals.The class Rational must have the following:

Data members:
private
int numerator
int denominator

Constructors
Default-argument constructor
Two-argument constructor to initialize numerator and denominator

Accessors (Getters)
public int getNumerator() - return the numerator
public int getDenominator() - return the denominator

Modifiers (Setters)
public void setNumerator(int value) - set the numerator to value
public void setDenominator(int value) - set the denominator to value

public inputRational()
Input the numerator and the denominator from the keyboard

public String toString()
return a string  in the form numerator/denominator

private int gcd(int m, int n) - return the greatest common divisor
int r;
while(n != 0)
{ r = m % n;
m = n;
n = r;
}
return m;

Add two Rational numbers
public void add(Rational, r1, Rational r2)

Subtract two Rational numbers
public Rational sub(Rational r2)

Multiply two Rational numbers
public void mul(Rational r1 , Rational r2)

Divide two Rational Numbers
public Rational div(Rational R)

Return the real value or Rational r1 divided by Rational r2
public static double divToDouble(Rational r1,Rational r2)

For the main method it should have:

Input Rational object R1

Input Rational object R2

Add the Rational objects R1 and R2 and store the result in the Rational object R3. Display the result in the Following Format:
numerator/denominator + numerator/denominator = numerator/denominator.

Subtract the Rational object R2 Rrom the Rational object R1 and store the result in the Rational object R3. Display the result in the following Format:
numerator/denominator - numerator/denominator = numerator/denominator.

Multiply the Rational object R1 with the Rational object R2 and store the result in the object R3. Display the result in the following Format:
numerator/denominator * numerator/denominator = numerator/denominator.

Divide the Rational object R1by the Rational object R2 and store the result in the object R3. Display the result in the following Format:
numerator/denominator / numerator/denominator = numerator/denominator.

Display the result or dividing the object R1 by the object R2 in a real number.

Change the numerator or object R1 to 2

Change the denominator or object R2 to 5

Display the numerator or R1 and the denominator or R2 in the following Format:
Numerator: ______
Denominator: _____

Explanation / Answer

import java.io.BufferedReader;

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

/**
*
* @author Sam
*/
public class Rational {
    private int numerator;
    private int denominator;

    public Rational() {
        numerator = 0;
        denominator = 0;
    }

    public Rational(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    public int getNumerator() {
        return numerator;
    }

    public void setNumerator(int numerator) {
        this.numerator = numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    public void setDenominator(int denominator) {
        this.denominator = denominator;
    }
  
    public void inputRational() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter numerator:");
        numerator = Integer.parseInt(br.readLine());
        System.out.println("Enter denominator:");
        denominator = Integer.parseInt(br.readLine());
    }

    @Override
    public String toString() {
        return numerator+"/"+denominator;
    }
  
    private int gcd(int m, int n) {
        int r;
        while(n != 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
  
    public void add(Rational r1, Rational r2) {
        denominator = r1.denominator * r2.denominator;
        numerator = r1.numerator * r2.denominator + r2.numerator * r1.denominator;
        System.out.println("n: " + numerator + " d: " + denominator);
        int gcd = gcd(numerator, denominator);
        System.out.println("gcd: " + gcd);
        denominator /= gcd;
        numerator /= gcd;
    }
  
    public void sub(Rational r1, Rational r2) {
        denominator = r1.denominator * r2.denominator;
        numerator = r1.numerator * r2.denominator - r2.numerator * r1.denominator;
        int gcd = gcd(numerator, denominator);
        denominator /= gcd;
        numerator /= gcd;
    }
  
    public void mul(Rational r1, Rational r2) {
        denominator = r1.denominator * r2.denominator;
        numerator = r1.numerator * r2.numerator;
        int gcd = gcd(numerator, denominator);
        denominator /= gcd;
        numerator /= gcd;
    }
  
    public void div(Rational r1, Rational r2) {
        denominator = r1.denominator * r2.numerator;
        numerator = r1.numerator * r2.denominator;
        int gcd = gcd(numerator, denominator);
        denominator /= gcd;
        numerator /= gcd;
    }
  
    public static double divToDouble(Rational r1, Rational r2) {
        int denominator = r1.denominator * r2.numerator;
        int numerator = r1.numerator * r2.denominator;
        return (double)numerator/denominator;
    }


    public static void main(String[] args) throws IOException {
        Rational R1 = new Rational();
        Rational R2 = new Rational();
      
        R1.inputRational();
        R2.inputRational();
      
        Rational tmp = new Rational();
      
        tmp.add(R1, R2);
        System.out.println(R1 + " + " + R2 + " = " + tmp);
      
        tmp.sub(R1, R2);
        System.out.println(R1 + " - " + R2 + " = " + tmp);
      
        tmp.mul(R1, R2);
        System.out.println(R1 + " * " + R2 + " = " + tmp);
      
        tmp.div(R1, R2);
        System.out.println(R1 + " / " + R2 + " = " + tmp);
      
        System.out.println(R1 + " / " + R2 + " = " + divToDouble(R1, R2));
      
        R1.setNumerator(2);
        R2.setDenominator(5);
      
        System.out.println("Numerator: " + R1.getNumerator());
        System.out.println("Denominator: " + R2.getDenominator());
    }
}

Let me know if you like the code!!