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

Question 1: We want to write a class for Rational numbers to be able to perform

ID: 3732486 • Letter: Q

Question

Question 1: We want to write a class for Rational numbers to be able to perform different mathematical operations such as addition, subtraction, multiplication, and etc. Here are the methods you need to implement for this class. Class Rationall public Rational (int numerator) I/A constructor that takes an integer as an input which is equal to a rational number with denominator being 1 public Rational (int numerator, int denominator): //A constructor that takes two integers. First integer is the numerator and the second integer is the denominator of the rational number. public Rational add (Rational other) Adds two rational numbers (two objects of the Rational class) and returns a Rational public Rational sub(Rational other)i I/Subtracts two rational numbers (two objects of the Rational class) and returns a Rational. public Rational mult (Rational other) I/Multiplies two rational numbers (two objects of the Rational class) and returns a Rational. public Rational divide (Rational other) //Divides two rational numbers (two objects of the Rational class) and returns a Rational object. Division by zero is not allowed. In that case, it should print "Unexpected Error: The denominator cannot be zero" and return null. public Rational oppositeO: //Negates the rational number. public Rational invert o: //Inverts the rational number. The inverse of zero does not exist. In that case, it should print "No inverse for zero!" And return null. ctice Problems -set 3, Page 1

Explanation / Answer

Hello, I have a solution for you. I have already answered a similar question once. So I’m making a few changes to it, which will satisfy your needs.

// Rational.java

public class Rational {

                private int numerator;

                private int denominator;

                public Rational(int numerator, int denominator) {

                                if (denominator == 0) {

                                                /**

                                                * making sure denominator never be 0 , if an invalid input is

                                                * given, setting denominator to 1

                                                */

                                                denominator = 1;

                                }

                                /**

                                * if denom is less than 0, making it positive and changing the sign of

                                * numerator

                                */

                                if (denominator < 0) {

                                                denominator = Math.abs(denominator);

                                                numerator = 0 - numerator;

                                }

                                /**

                                * setting validated values to the variables

                                */

                                this.numerator = numerator;

                                this.denominator = denominator;

                }

                /**

                * A private method to calculate the greatest common divisor of two numbers

                */

                private int gcd(int x, int y) {

                                if (y == 0)

                                                return x;

                                return gcd(y, x % y);

                }

                /**

                * method to add a rational number to the current number and returns it

                */

                public Rational add(Rational b) {

                                int num = (this.numerator * b.denominator)

                                                                + (this.denominator * b.numerator);

                                int den = this.denominator * b.denominator;

                                Rational rational = new Rational(num, den);

                                return rational;

                }

                /**

                * method to subtract a rational number from the current number and returns

                * it

                */

                public Rational sub(Rational b) {

                                int num = (this.numerator * b.denominator)

                                                                - (this.denominator * b.numerator);

                                int den = this.denominator * b.denominator;

                                Rational rational = new Rational(num, den);

                                return rational;

                }

                /**

                * method to multiply a rational number to the current number and returns it

                */

                public Rational mult(Rational b) {

                                int num = this.numerator * b.numerator;

                                int den = this.denominator * b.denominator;

                                Rational rational = new Rational(num, den);

                                return rational;

                }

                /**

                * method to divide a rational number from the current number and returns it

                */

                public Rational divide(Rational b) {

                                if (b.numerator == 0) {

                                                System.out

                                                                                .println("Unexpected Error: The denominator cannot be zero");

                                                return null;

                                }

                                /**

                                * Finding the recipocal of second number and multiplying it

                                */

                                b = b.invert();

                                return mult(b);

                }

                /**

                * method to find and return the reciprocal of a rational number

                */

                public Rational invert() {

                                if (numerator == 0) {

                                                System.out.println("No inverse for zero");

                                                return null;

                                }

                                int newNumerator = this.denominator;

                                int newDenominator = Math.abs(this.numerator);

                                if (this.numerator < 0) {

                                                newNumerator = 0 - newNumerator;

                                }

                                return new Rational(newNumerator, newDenominator);

                }

                /**

                * method to find and return the opposite of a number (negate)

                */

                public Rational opposite() {

                                Rational opposite = new Rational(-numerator, denominator);

                                return opposite;

                }

                /**

                * method to simplify a rational number and returns it. This method will

                * find the gcd and divides it from bothy numerator and denominator

                */

                public Rational simplify() {

                                /**

                                * Finding the GCD

                                */

                                int gcd = gcd(numerator, denominator);

                                /**

                                * removing the GCD from numer..and denom..

                                */

                                int newNumerator = numerator / gcd;

                                int newDenominator = denominator / gcd;

                                Rational simplified = new Rational(newNumerator, newDenominator);

                                return simplified;

                }

                /**

                * calculates and returns the power of rational number upto the given number

                */

                public Rational power(int n) {

                                /**

                                * applying the power to numerator and denominator

                                */

                                int newNumerator = (int) Math.pow(numerator, n);

                                int newDenominator = (int) Math.pow(denominator, n);

                                Rational newRational = new Rational(newNumerator, newDenominator);

                                return newRational;

                }

                /**

                * method to return a string representation of a number

                */

                public String toString() {

                                return numerator + "/" + denominator;

                }

}

// Test.java

public class Test {

                public static void main(String[] args) {

                                /**

                                * Defining two rational numbers and performing all defined operations

                                */

                                Rational r1 = new Rational(10, 5);

                                Rational r2 = new Rational(9, 2);

                                System.out.println("r1: " + r1);

                                System.out.println("r2: " + r2);

                                System.out.println("r1 simplified: " + r1.simplify());

                                System.out.println("r2 simplified: " + r2.simplify());

                                System.out.println("r1+r2 (simplified) :" + r1.add(r2).simplify());

                                System.out.println("r1-r2 (simplified) :" + r1.sub(r2).simplify());

                                System.out.println("r1*r2 (simplified) :" + r1.mult(r2).simplify());

                                System.out.println("r1/r2 (simplified) :" + r1.divide(r2).simplify());

                                System.out.println("r1^2 (simplified) :" + r1.power(2).simplify());

                                System.out.println("The opposite of " + r1 + " is " + r1.opposite());

                                System.out.println("The inverse of " + r1 + " is " + r1.invert());

                }

}

/*OUTPUT*/

r1: 10/5

r2: 9/2

r1 simplified: 2/1

r2 simplified: 9/2

r1+r2 (simplified) :13/2

r1-r2 (simplified) :-5/2

r1*r2 (simplified) :9/1

r1/r2 (simplified) :4/9

r1^2 (simplified) :4/1

The opposite of 10/5 is -10/5

The inverse of 10/5 is 5/10

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