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

JAVA PROBLEM Create a class called Rational for performing arithmetic with fract

ID: 3702992 • Letter: J

Question

JAVA PROBLEM

Create a class called Rational for performing arithmetic with fractions.

Write a program to test your class.

Use integer variables to represent the private instance variables of the class - the numerator and the denominator.

Provide a constructor that enables an object of this class to be initialized when it’s declared.

The constructor should store the fraction in reduced form.

The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator.

Provide a no-argument constructor with default values in case no initializers are provided.

Provide public methods that perform each of the following operations:

a) Add two Rational numbers: The result of the addition should be stored in reduced form.

b) Subtract two Rational numbers: The result of subtraction should be stored in reduced form.

c) Multiply two Rational numbers: The result of the multiplication should be stored in reduced form.

d) Divide two Rational numbers: The result of the division should be stored in reduced form.

e) Return a String representation of Rational number in the form ??/??, where ?? is the numerator and ?? is the denominator.

Here is a sample run:

Enter numerator for the first rational number: 4

Enter a non-zero denominator for the first rational number: 6

Enter numerator for the second rational number: 1

Enter a non-zero denominator for the second rational number: 4

First rational number is: 2/3

Second rational number is: 1/4

Addition of the rational numbers is: 11/12

Subtraction of the rational numbers is: 5/12

Multiplication of the rational numbers is: 1/6

Explanation / Answer

Rational.java

public class Rational {

private int numerator, denominator;

// -----------------------------------------------------------------

// Sets up the rational number by ensuring a nonzero denominator

// and making only the numerator signed.

// -----------------------------------------------------------------

public Rational(int numer, int denom) {

if (denom == 0)

denom = 1;

// Make the numerator "store" the sign

if (denom < 0) {

numer = numer * -1;

denom = denom * -1;

}

if (numer < 0 && denom < 0) {

numer = numer * -1;

denom = denom * -1;

}

numerator = numer;

denominator = denom;

reduce();

}

// -----------------------------------------------------------------

// Returns the numerator of this rational number.

// -----------------------------------------------------------------

public int getNumerator() {

return numerator;

}

// -----------------------------------------------------------------

// Returns the denominator of this rational number.

// -----------------------------------------------------------------

public int getDenominator() {

return denominator;

}

// -----------------------------------------------------------------

// Adds this rational number to the one passed as a parameter.

// A common denominator is found by multiplying the individual

// denominators.

// -----------------------------------------------------------------

public static Rational add(Rational op1, Rational op2) {

int num1 = op1.getNumerator();

int denom1 = op1.getDenominator();

int num2 = op2.getNumerator();

int denom2 = op2.getDenominator();

int num3 = (num1 * denom2) + (num2 * denom1);

int denom3 = denom1 * denom2;

Rational r1 = new Rational(num3, denom3);

return r1;

}

// -----------------------------------------------------------------

// Subtracts the rational number passed as a parameter from this

// rational number.

// -----------------------------------------------------------------

public static Rational subtract(Rational op1, Rational op2) {

int commonDenominator = op1.getDenominator() * op2.getDenominator();

int numerator1 = op1.getNumerator() * op2.getDenominator();

int numerator2 = op2.getNumerator() * op1.getDenominator();

int difference = numerator1 - numerator2;

return new Rational(difference, commonDenominator);

}

// -----------------------------------------------------------------

// Multiplies this rational number by the one passed as a

// parameter.

// -----------------------------------------------------------------

public static Rational multiply(Rational op1, Rational op2) {

int numer = op1.getNumerator() * op2.getNumerator();

int denom = op1.getDenominator() * op2.getDenominator();

return new Rational(numer, denom);

}

// This method checks Whether the two Rational Numbers are equal or not

public static boolean equals(Rational op1, Rational op2) {

if (op1.getNumerator() * op2.getDenominator() == op1.getDenominator() * op2.getNumerator())

return true;

else

return false;

}

// -----------------------------------------------------------------

// Reduces this rational number by dividing both the numerator

// and the denominator by their greatest common divisor.

// -----------------------------------------------------------------

public void reduce() {

if (numerator != 0) {

int common = gcd(Math.abs(numerator), denominator);

numerator = numerator / common;

denominator = denominator / common;

}

}

// -----------------------------------------------------------------

// Computes and returns the greatest common divisor of the two

// positive parameters. Uses Euclid's algorithm.

// -----------------------------------------------------------------

private int gcd(int num1, int num2) {

// % is modulus which is the remainder of a division

// base case

if ((num1 % num2) == 0) {

return num2;

}

// recursive case

else {

return gcd(num2, num1 % num2);

}

}

// -----------------------------------------------------------------

// Returns this rational number as a string.

// -----------------------------------------------------------------

public String toString() {

String result;

if (numerator == 0)

result = "0";

else if (denominator == 1)

result = numerator + "";

else

result = numerator + "/" + denominator;

return result;

}

}

____________________

Test.java

import java.util.Scanner;

import sun.awt.geom.AreaOp.AddOp;

public class Test {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int num1,num2,denom1,denom2;

  

//Getting the First Rational number From the user

System.out.print("Enter numerator for the first rational number:");

num1 = sc.nextInt();

System.out.print("Enter a non-zero denominator for the first rational number:");

denom1 = sc.nextInt();

//Getting the second Rational number From the user

System.out.print("Enter numerator for the second rational number:");

num2 = sc.nextInt();

System.out.print("Enter a non-zero denominator for the second rational number:");

denom2 = sc.nextInt();

//Creating an Instance of Rational Class objects

Rational r1=new Rational(num1, denom1);

Rational r2=new Rational(num2, denom2);

//Displaying the Rational Numbers

System.out.println("First rational number is :"+r1);

System.out.println("Second rational number is :"+r2);

//Calling the add,subtract,multiply methods on Rational Class

System.out.println("Addition of the rational numbers is: "+Rational.add(r1, r2));

System.out.println("Subtraction of the rational numbers is: "+Rational.subtract(r1,r2));

System.out.println("Multiplication of the rational numbers is: "+Rational.multiply(r1,r2));

}

}

____________________

Output:

Enter numerator for the first rational number:4
Enter a non-zero denominator for the first rational number:6
Enter numerator for the second rational number:1
Enter a non-zero denominator for the second rational number:4
First rational number is :2/3
Second rational number is :1/4
Addition of the rational numbers is: 11/12
Subtraction of the rational numbers is: 5/12
Multiplication of the rational numbers is: 1/6

____________Thank You