RationalNumber Write a class called RationalNumber that repr

RationalNumber

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

Constructs a new rational number to represent the ratio (numerator/denominator). The denominator cannot be 0, so throw an IllegalArgumentException if 0 is passed.

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

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

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

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\".

Returns true if this and other are equivalent RationalNumbers

Returns rational number that is this + other.

Returns rational number that is this - other.

Returns rational number that is this * other.

Returns rational number that is this / other.

Your RationalNumber objects should always be in reduced form, avoiding rational numbers such as 3/6 in favor of 1/2, or avoiding 2/–3 in favor of –2/3. This may require the implementation of another method. Consider the Greatest Common Divisor (GCD) definition: The GCD of two integers a and b is the largest integer that is a factor of both a and b. One efficient way to compute the GCD of two numbers is to use Euclid’s algorithm, which states the following:

Solution

package org.students;

public class RationalNumber {

   private int numerator;
   private int denominator;
   public RationalNumber()
   {
       this.numerator=0;
       this.denominator=1;
   }
   public RationalNumber(int numerator, int denominator) {
       super();
       this.numerator = numerator;
       if(denominator==0)
           throw new IllegalArgumentException(\":: The Denominator cannot be Zero ::\");
       else
       this.denominator = denominator;
   }
   public int getNumerator() {
       return numerator;
   }

   public int getDenominator() {
       return denominator;
   }
   @Override
   public String toString() {
       return \"RationalNumber [numerator=\" + numerator + \", denominator=\"
               + denominator + \"]\";
   }
  
  

}

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site