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

Rational Implement a class to represent rational numbers. Each rational number c

ID: 3786894 • Letter: R

Question

Rational

Implement a class to represent rational numbers. Each rational number consists of a numerator and a denominator, both of type int. Since each rational number has its own numerator and denominator, these must be instance variables. Furthermore, good object-oriented programming suggests that the visibility of the variables should be private.

2.1 Constructors

The class Rational has two constructors. One of them has two formal parameters, which provide the initial values for both instance variables. The other constructor has a single parameter, which provides the initial value for the numerator; the denominator is assumed to be 1.

2.2 getters

Implement access methods that return the numerator and denominator of this rational, but no setter methods. An object that has no setter methods, and no other methods for transforming the state of the object, is said to be immutable. Immutable is a great property. Do you see why? Discuss this with your neighbors and TA.

2.3 plus

Implement the instance method plus. The method has a single formal parameter, of type Rational. The method returns a new Rational object that represents the sum of this number and that of the parameter.

2.4 plus (part 2)

Implement a class method plus. The method has two formal parameters, both of type Rational. The method returns a new Rational object that represents the sum of the two numbers.

2.5 gcd

Implement a private class method for calculating the greatest common divisor of two integers, which are the formal parameters of the method.

2.6 reduce

Implement a private instance method called reduce that transforms this number into its reduced form. (ex: 18/4 -> 9/2)

2.7 reduce (part 2)

Make all the necessary changes so that a rational number is always stored in reduced form.

2.8 equals

Implement the instance method public boolean equals(Rational o) that returns true if this fraction and the one designated by 'o' represent the same fraction (content equality).

2.9 toString

Add a method public String toString() that returns a String representation of this fraction with the numerator followed by the symbol “/”, followed by the denominator. If the denominator is 1 then the method returns a String consisting of the numerator only.

2.10 compareTo

Implements the instance method int compareTo( Rational o ). It compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Explanation / Answer

Rational.java

public class Rational {
private int numerator, denominator;
  
//Parameterized constructor
public Rational(int a, int b){
numerator = a;
denominator = b;

if (denominator < 0){
denominator = denominator * -1;
numerator = numerator * -1;
}else if (denominator == 0){
denominator = 1;
numerator = 0;
}
Rational.this.reduce();

}
//Default constructor
public Rational(){
denominator = 1;
numerator = 0;
}
//Setters and Getters
public void setNumerator(int a){
numerator = a;
Rational.this.reduce();


}

public int getNumerator(){
return numerator;
}

public void setDenominator(int b){
if (b < 0){
b = b * -1;
denominator = b;
if (numerator < 0){
;
}else{
numerator = getNumerator() * -1;

}

}else if (b == 0){
setNumerator(0);
setDenominator(1);

}else{
denominator = b;
}
Rational.this.reduce();

}
public int getDenominator(){
return denominator;
}
//Method which displays the Rational Number
public String toString(){
String stringFormat = numerator + " / " + denominator;
return stringFormat;
}
//Method which comparing two rational numbers
public int compareTo(Rational a){
int thisRational = numerator * a.getDenominator();
int otherRational = a.getNumerator() * denominator;
if (thisRational < otherRational){
return -1;
}else if (thisRational > otherRational ){
return 1;
}else{
return 0;
}
}
//Method which adding two rational numbers
public Rational plus(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = (num1 * denom2) + (num2 * denom1);
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}

//Method which reduce the rational number
public String reduce( )
{
  
       // determine the greatest common divisor
       int gcd = this.gcd(numerator, denominator);
       // if GCD is negative, change to positive
       if (gcd < 0)
       {
       gcd = -gcd;
       }
       // divide gcd into both numerator and denominator
       numerator = numerator / gcd;
       denominator = denominator / gcd;
      
return numerator+"/"+denominator;
}
//Method which calculate the GCD
private Integer gcd(Integer a, Integer b)
{
   // "private"
   // % is modulus which is the remainder of a division
   // base case
   if ((a % b) == 0) {
   return b;
   }
   // recursive case
   else {
   return gcd(b, a % b);
   }
   }

//This method checks Whether the two Rational Numbers are equal or not
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Rational other = (Rational) obj;
       if (denominator != other.denominator)
           return false;
       if (numerator != other.numerator)
           return false;
       return true;
   }
  
}

____________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       Scanner sc1 = new Scanner(System.in);
       while (true) {
           //Getting the First Rational number From the user
           System.out.println(":: Enter Rational No 1 ::");
           System.out.print("Enter Numerator =");
           int num1 = sc.nextInt();
           System.out.print("Enter Denominator =");
           int denom1 = sc.nextInt();

           //Getting the second Rational number From the user
           System.out.println(":: Enter Rational No 2 ::");
           System.out.print("Enter Numerator =");
           int num2 = sc.nextInt();
           System.out.print("Enter Denominator =");
           int denom2 = sc.nextInt();
  
           //Creating the objects to Rational Class
           Rational r = new Rational(num1, denom1);
           Rational r1 = new Rational(num2, denom2);
           System.out.println(" ");
          
          
           // Reducing the Rational Numbers
           System.out.println("** Rational Reduced Forms **");
           String red = r.reduce();
           String red1 = r1.reduce();
           System.out.println("(" +num1+"/"+denom1+") reduced to :" + red);
           System.out.println("(" +num2+"/"+denom2+") reduced to :" + red1);

          
           System.out.println(" ** Adding two Rational Numbers **");
           // Addition of two Rational Numbers
           Rational radd = r.plus(r1);
           System.out.println("(" + r.toString() + ") +" + "(" + r1.toString()+ ") = " + radd);

          
           System.out.println(" ** Comparing two Rational Numbers **");
           // Comparing Two Rational Numbers
           int com = r.compareTo(r1);
           if (com == -1)
               System.out.println("(" + r.toString() + ") < (" + r1.toString()+ ")");
           else
               System.out.println("(" + r.toString() + ") > (" + r1.toString()
                       + ")");

          
          
           System.out.println(" ** Checking two Rational Numbers are equal or not **");
           boolean bool=r.equals(r1);
           if(bool==true)
               System.out.println(r.toString()+" is equal to "+r1.toString());
           else
               System.out.println(r.toString()+" is not equal to "+r1.toString());
           System.out.println(" ");
          
          
          
           System.out.print("Do you want to continue(Y/N):");
           char c = sc1.next(".").charAt(0);
           if (c == 'Y' || c == 'y')
               continue;
           else {
               System.out.println(":: Program Exit ::");
               break;
           }

       }

   }

}

________________________

output:

:: Enter Rational No 1 ::
Enter Numerator =5
Enter Denominator =25
:: Enter Rational No 2 ::
Enter Numerator =25
Enter Denominator =125

** Rational Reduced Forms **
(5/25) reduced to :1/5
(25/125) reduced to :1/5

** Adding two Rational Numbers **
(1 / 5) +(1 / 5) = 2 / 5

** Comparing two Rational Numbers **
(1 / 5) > (1 / 5)

** Checking two Rational Numbers are equal or not **
1 / 5 is equal to 1 / 5

Do you want to continue(Y/N):n
:: Program Exit ::

_____________Thank You