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

Write a class called RationNumber that represents a fraction with an integer num

ID: 3541365 • Letter: W

Question

Write a class called RationNumber that represents a fraction with an integer numerator and denominator. A RationalNumber object should have the following methods:


public RationalNumber(int numerator, int denominator)

Constructs a new rational number to represent the ratio (numerator/denominator). The denominator cannot be 0.

so throw an IllegalArgumentException if 0 is passed.

public RationalNumber ()

Constructs a new rational number to represent the ratio (0/1).

public int getDenominator()

Returns this rational number's denominator value; for example, if the ratio is (3/5), returns 5.

public int getNumerator()

Returns this rational number's numerators value; for example, if the ratio is (3/5), returns 3.

public String toString()

Returns a String representation of this rational number, such as "3/5". You may wish to omit denominators of 1.

returning "4" instead of "4/1".

An extra challenge would be to maintain your RationalNumber objects in reduced form, avoiding rational numbers such as 3/6 in favor of 1/2, or avoiding 2/-3. Another possible extra feature would be methods to add, subtract, multiply, and divide two rational numbers.


import java.util.*;


public class RationalNumber{

public int numerator

public int denominator

public int x

public int y

}


public RationalNumber(int numerator, int denominator)


// returns the greatest common divisor of x and y

private int gcd(int x, int y) {

while (y != 0) {

int temp = x % y;

x = y;

y = temp;

        }

        return x;

   


public int getNumerator();

return = new Numerator


public int getDenominator();

return = new Denominator


public String toString();


Explanation / Answer

Hi,

I have completed the code. Plese drop me a mail.

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