In Java Write a class definition for a class of Rational num

In Java:

Write a class definition for a class of Rational numbers. Your class should have data fields for
numerator and denominator. Also, include methods for all associated mathematical functions, and the Rational number should always be reduced to its lowest form. Consider the following:

How can you avoid a “divide by zero” issue? (denominator = 0) What does the default constructor look like when you define an object as: Rational x = new Rational(); How will you handle: Rational x = new Rational(4, 8); ?
Write a test program to do the following:
X = ¼ + ½ X = ½ - ¼ X = ½ * ¼ X = ¼ / ½

Solution

public class Rational {

   private static Rational zero = new Rational(0, 1);

   private int num; // the numerator
   private int den; // the denominator

   public Rational() {
       this.num = 0;
       this.den = 1;
   }

   // create and initialize a new Rational object
   public Rational(int numerator, int denominator) {

       // deal with x/0
       // if (denominator == 0) {
       // throw new RuntimeException(\"Denominator is zero\");
       // }

       // reduce fraction
       int g = gcd(numerator, denominator);
       num = numerator / g;
       den = denominator / g;

       // only needed for negative numbers
       if (den < 0) {
           den = -den;
           num = -num;
       }
   }

   // return the numerator and denominator of (this)
   public int numerator() {
       return num;
   }

   public int denominator() {
       return den;
   }

   // return double precision representation of (this)
   public double toDouble() {
       return (double) num / den;
   }

   // return string representation of (this)
   public String toString() {
       if (den == 1)
           return num + \"\";
       else
           return num + \"/\" + den;
   }

   // return gcd(|m|, |n|)
   private static int gcd(int m, int n) {
       if (m < 0)
           m = -m;
       if (n < 0)
           n = -n;
       if (0 == n)
           return m;
       else
           return gcd(n, m % n);
   }

   // return { -1, 0, +1 } if a < b, a = b, or a > b
   public int compareTo(Rational b) {
       Rational a = this;
       int lhs = a.num * b.den;
       int rhs = a.den * b.num;
       if (lhs < rhs)
           return -1;
       if (lhs > rhs)
           return +1;
       return 0;
   }

   // return lcm(|m|, |n|)
   private static int lcm(int m, int n) {
       if (m < 0)
           m = -m;
       if (n < 0)
           n = -n;
       return m * (n / gcd(m, n)); // parentheses important to avoid overflow
   }

   // return a + b, staving off overflow
   public Rational plus(Rational b) {
       Rational a = this;

       // special cases
       if (a.compareTo(zero) == 0)
           return b;
       if (b.compareTo(zero) == 0)
           return a;

       // Find gcd of numerators and denominators
       int f = gcd(a.num, b.num);
       int g = gcd(a.den, b.den);

       // add cross-product terms for numerator
       Rational s = new Rational((a.num / f) * (b.den / g) + (b.num / f)
               * (a.den / g), lcm(a.den, b.den));

       // multiply back in
       s.num *= f;
       return s;
   }

   // return -a
   public Rational negate() {
       return new Rational(-num, den);
   }

   // return a - b
   public Rational minus(Rational b) {
       Rational a = this;
       return a.plus(b.negate());
   }

   public Rational reciprocal() {
       return new Rational(den, num);
   }

   // return a / b
   public Rational divides(Rational b) {
       Rational a = this;
       return a.times(b.reciprocal());
   }

   // return a * b, staving off overflow as much as possible by
   // cross-cancellation
   public Rational times(Rational b) {
       Rational a = this;

       // reduce p1/q2 and p2/q1, then multiply, where a = p1/q1 and b = p2/q2
       Rational c = new Rational(a.num, b.den);
       Rational d = new Rational(b.num, a.den);
       return new Rational(c.num * d.num, c.den * d.den);
   }

   public static void main(String[] args) {

       Rational x1 = new Rational();
       Rational x2 = new Rational(4, 8);
       Rational x3 = new Rational(1, 4);
       Rational x4 = new Rational(1, 2);
       System.out.println(\"x1:\" + x1);
       System.out.println(\"x2:\" + x2);
       System.out.println(\"x3:\" + x3);
       System.out.println(\"x4:\" + x4);

       Rational add = x3.plus(x4);
       System.out.println(\"Addition of x3 and x4 :\" + add);

       Rational sub = x3.minus(x4);
       System.out.println(\"Substraction of x3 and x4 :\" + sub);

       Rational mult = x3.times(x4);
       System.out.println(\"Mutiplication of x3 and x4 :\" + mult);

       Rational div = x3.divides(x4);
       System.out.println(\"Divison of x3 and x4 :\" + div);

   }

}

OUTPUT:

x1:0
x2:1/2
x3:1/4
x4:1/2
Addition of x3 and x4 :3/4
Substraction of x3 and x4 :-1/4
Mutiplication of x3 and x4 :1/8
Divison of x3 and x4 :1/2

In Java: Write a class definition for a class of Rational numbers. Your class should have data fields for numerator and denominator. Also, include methods for a
In Java: Write a class definition for a class of Rational numbers. Your class should have data fields for numerator and denominator. Also, include methods for a
In Java: Write a class definition for a class of Rational numbers. Your class should have data fields for numerator and denominator. Also, include methods for a
In Java: Write a class definition for a class of Rational numbers. Your class should have data fields for numerator and denominator. Also, include methods for a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site