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

****Using DrJava please and it needs to be compiled and runned correctly**** ***

ID: 3768489 • Letter: #

Question

****Using DrJava please and it needs to be compiled and runned correctly****

*****Include Javadoc comments please with the code to understand the steps. I will most definietly rate and give you the best answer******

Program Names: Rational.java and RationalDriver.java

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, ½, ¾, and so forth are all rational numbers (by ½ and so forth, we mean the mathematical meaning of the fraction, not the integer division this expression would produce in a Java program.) Represent rational numbers as two values of type int, one for the numerator, and one for the denominator. Your class should have two instance variables of type int. The name of the class is Rational. Include a default constructor and an overloaded constructor with two arguments that are used to set the instance variables of an object of the Rational class to any two integer values. Note that the numerator, the denominator, or both may be negative.

You should include a method to normalize the sign of the rational number so that the denominator is always positive and the numerator is either positive or negative. For example, after normalization, 4/-8 would be the same as –4/8.

Define the following methods (using the names in parenthesis) for addition (add), subtraction (subtract), multiplication (multiply), and division (divide) of objects of your class, Rational. Each of these methods must accept a Rational object as the single parameter. Arithmetic operations on fractions are defined by the following rules:

a/b + c/d = (ad + bc) / bd

a/b –c/d = (ad-bc) / bd

a/b * c/d = ac/db

(a/b) / (c/d) = ad / bc, where c/d 0

These are instance methods with a single argument that do not return a value. The result is a change in the state of the value of the data members of the calling object. For example, the add method (for addition) has a calling object and one argument. Therefore, if rationalNum1 has a numerator value of 1 and a denominator value of 2 and rationalNum2 has a numerator value of 1 and a denominator value of 4, the method call,

rationalNum1.add(rationalNum2);

changes the values of the instance variables of rationalNum1 so they represent the result of adding rationalNum2 to the original version of rationalNum1 (numerator is 3 and denominator is 4).

Define observer (getter) and mutator (setter) methods. In addition, define the following method:

• public boolean equals(Object other)

• public String toString()

Hint: Two rational numbers a/b and c/d are equal if a*d equals c*b. Guidelines for overriding the equals method:

Use the == operator to check if the argument is a reference to itself.

• Use the instanceof operator to check if the argument has the correct data type.

• Cast the argument to the correct type.

For each significant data member, test for equality.

• Test these three properties:

o Is it symmetric?

o Is it transitive?

o Is it consistent?

Write a second class called RationalDriver containing a main method.

Your main method will allow the user to thoroughly test all of the methods in your Rational class. The main method should take keyboard input from the user; instantiate two Rational objects, and then ask the user which operations to perform. Allow the user to continue performing operations on rational numbers until the user elects to quit. For example:

Enter the numerator for rational number #1:

Enter the denominator for rational number #1:

Enter the numerator for rational number #2:

Enter the denominator for rational number #2:

Enter the corresponding number for the desired action,

1. Addition

2. Subtraction

3. Multiplication

4. Division

5. Test for Equality

6. Change 1st rational number

7. Change 2nd rational number

8. Exit

Develop this program in stages:

1. Default and Parameterized Constructors

2. toString()to display fraction - print out fraction to test it

3. Normalize fraction after constructor executes

4. Implement method – print out results

5. Repeat…. for the remaining project requirements

6. Loop to allow multiple rational numbers to be input

Your file names should be Rational.java and RationalDriver.java.

Test your program with several different data sets. Document your tests on the attached table and submit the plan with copies of your source code files.

rationalNum1

rationalNum2

Action Performed

Expected Change

Actual Change

rationalNum1

rationalNum2

Action Performed

Expected Change

Actual Change

Explanation / Answer

RationalDriver.java
import java.util.Arrays;
public class RationalDriver
{
   public static void main (String[] args)
   {

      RationalNumber rnumber0 = new RationalNumber();
      RationalNumber rnumber1 = new RationalNumber(6, 8);
      RationalNumber rnumber2 = new RationalNumber(1, -3);
      RationalNumber rnumber8 = new RationalNumber(0, 14);
      RationalNumber rnumber3, rnumber4, rnumber5, rnumber6, rnumber7;
      System.out.println ("The First rational number: " + rnumber1.toString());
      System.out.println ("The Second rational number: " + rnumber2);
      System.out.println ("The Third rational number: " + rnumber8);
      if (rnumber1.equals(rnumber2))
      {
         System.out.println ("rnumber1 and rnumber2 are equal.");
      }
      else
      {
         System.out.println ("rnumber1 and rnumber2 are NOT equal.");
      }

      rnumber3 = rnumber1.reciprocal();
      System.out.println ("The reciprocal of rnumber1 is: " + r3);
  
      rnumner4 = rnumber1.add(r2);
      rnumber5 = rnumber1.subtract(r2);
      rnumber6 = rnumber1.multiply(r2);
      rnumber7 = rnumber1.divide(r2);
      rnumber8 = rnumber1.multiply(3, 4);

      System.out.println ("rnumber1 + rnumber2: " + rnumber4);
      System.out.println ("rnumber1 - rnumber2: " + rnumber5);
      System.out.println ("rnumber1 * rnumber2: " + rnumber6);
      System.out.println ("rnumber1 / rnumber2: " + rnumber7);
      System.out.println ("rnumber1 * 3/4: " + rnumber8);
      rnumber1 = null;
      rnumber2 = null;
    
  
      RationalNumber[] rnumberArray = new RationalNumber[5];
      System.out.println(rnumberArray.length);
      rnumberArray[0] = new RationalNumber(1, 2);
      rnumberArray[1] = new RationalNumber(2, 7);
      rnumberArray[2] = new RationalNumber(99, 100);
      rnumberArray[3] = new RationalNumber(3, 4);
      rnumberArray[4] = new RationalNumber(1, 3);
      rnumberArray[0].setCompareField(1);
      for (int i = 0; i < rnumberArray.length; i++)
      {
          System.out.println(rnumberArray[i]);
      }
      Arrays.sort(rnumberArray);
      System.out.println("After sorting the");
      for (RationalNumber r : rnumberArray)
      {
          System.out.println(r);
      }
   }
}

Rational.java
import java.util.Arrays;
public class Rational {
private int n;
private int d;
public Rational(int numerator, int denominator) {
        if (denominator == 0) {
            throw new RuntimeException("The Denominator is zero");
        }
        int g11 = gcd(numerator, denominator);
        n = numerator   / g11;
        d = denominator / g11;

    }
      public String toString() {
        if (d == 1) return n + "";
        else return n + "/" + d;
    }

     public Rational times(Rational r) {
        return new Rational(this.n * r.n, this.d * r.d);
    }

    public Rational plus(Rational r) {
        int numerator   = (this.n * r.d) + (this.d * r.n);
        int denominator = this.d * r.d;
        return new Rational(numerator, denominator);
    }

    public Rational reciprocal() {
return new Rational(d, n); }

     public Rational divides(Rational r) {
        return this.times(r.reciprocal());
    }

    private static int gcd(int m11, int n11) {
        if (0 == n11) return m11;
        else return gcd(n11, m11 % n11);
    }

    public static void main(String[] args) {
        Rational x11, y11, z11;
        x11 = new Rational(1, 2);
        y11 = new Rational(1, 3);
        z11 = x11.plus(y11);
        System.out.println(z11);

        x11 = new Rational(8, 9);
        y11 = new Rational(1, 9);
        z11 = x11.plus(y11);
        System.out.println(z11);

     
        x11 = new Rational(4, 17);
        y11 = new Rational(7, 3);
        z11 = x11.times(y11);
        System.out.println(z11);

      
        x11 = new Rational(203, 16957);
        y11 = new Rational(9299, 5887);
        z11 = x11.times(y11);
        System.out.println(z11);

      
        x11 = new Rational(0, 6);
        System.out.println(x11);

    }

}