can you solve this in Implement a data type Rational in Rati

can you solve this in

Implement a data type Rational in Rational.java that represents a Remote Disc rational number, i.e., a number of the form a/b where a and b notequalto 0 are integers. The data type must support the following API:

Solution

Program:

Implement the main Method Accodingly


public class Rational {
   private long x;
   private long y;

   public Rational(long x) {
       this.x = x;
       this.y = 1;
   }

   public Rational(long x, long y) {
       if (y < 0) {
           x = x * -1;
           y = y * -1;
       }
       this.x = x;
       this.y = y;
   }

   public Rational add(Rational that) {
       long commonDenominator = this.y * that.y;
       long numerator1 = this.x * that.y;
       long numerator2 = that.x * this.y;
       long sum = numerator1 + numerator2;

       return new Rational(sum, commonDenominator);
   }

   public Rational multiply(Rational that) {
       long numer = this.x * that.x;
       long denom = this.y * that.y;

       return new Rational(numer, denom);
   }

   private static long gcd(long p, long q) {
       while (p != q)
           if (p > q)
               p = p - q;
           else
               q = q - p;

       return p;
   }

   public String toString() {
       String result;

       if (x == 0)
           result = \"0\";
       else if (y == 1)
           result = x + \"\";
       else
           result = x + \"/\" + y;

       return result;
   }

   public static void main(String[] args) {

   }

}

can you solve this in Implement a data type Rational in Rational.java that represents a Remote Disc rational number, i.e., a number of the form a/b where a and
can you solve this in Implement a data type Rational in Rational.java that represents a Remote Disc rational number, i.e., a number of the form a/b where a and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site