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

Define a class using Test Driven Development that models a rational number. A ra

ID: 3791819 • Letter: D

Question

Define a class using Test Driven Development that models a rational number. 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. It will have a default, parameterless 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 = (a*d + b*c) / b*d

a/b – c/d = (a*d – b*c) / b*d

a/b * c/d = a*c / d*b

(a/b) / (c/d) = a*d / b*c, 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, and override the equals and toString methods. Hint: Two rational numbers a/b and c/d are equal if a*d equals c*b.

Your file names should be Rational.java and RationalTest.java for the jUnit test class

Explanation / Answer

Hi buddy, Please find the below java program. I've added comments for your better understanding

import java.util.*;
import java.lang.*;
import java.io.*;

class Rational{
    private int num,den;
  
    //This method finds the gcd of 2 numbers.
    static public int gcd(int a, int b){
        if(b==0){
            return a;
        }
        return gcd(b,a%b);
    }
  
    public Rational(){
      
    }
  
    //This method normalizes the rational number
    public void normalize(){
        if(this.den<0){
            this.num = -this.num;
            this.den = -this.den;
        }
    }
    //This is a parameterized constructor
    public Rational(int num, int den){
        this.num = num;
        this.den = den;
        normalize();
    }
  
    //This method adds another rational number to this rational number
    public void add(Rational a){
        int num = this.num*a.den + this.den*a.num;
        int den = this.den * a.den;
        int g = gcd(num,den);
        this.num = num/g;
        this.den = den/g;
        this.normalize();
    }
    //This method substracts another rational number from this rational number
    public void substract(Rational a){
        int num = this.num*a.den - this.den*a.num;
        int den = this.den * a.den;
        int g = gcd(num,den);
        this.num = num/g;
        this.den = den/g;
        this.normalize();
    }
    //This method multiplies another rational number with this rational number
    public void multiply(Rational a){
        int num = this.num*a.num;
        int den = this.den * a.den;
        int g = gcd(num,den);
        this.num = num/g;
        this.den = den/g;
        this.normalize();
    }
    //This method divides another rational number with this rational number
    public void divide(Rational a){
        int num = this.num*a.den;
        int den = this.den*a.num;
        int g = gcd(num,den);
        this.num = num/g;
        this.den = den/g;
        this.normalize();
    }
    @Override
    public String toString(){
        return this.num+"/"+this.den;
    }
    @Override
    public boolean equals(Object c){
        Rational b = (Rational)c;
        return this.num==b.num && this.den == b.den;
    }
    //These are getter methods
    public int getNumerator(){
        return this.num;
    }
    public int getDenominator(){
        return this.den;
    }
    //These are setter methods
    public void setNumerator(int num){
        this.num = num;
    }
    public void setDenominator(int den){
        this.den = den;
    }
  
}
class RationalTest
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // Creating two rational numbers for testing purpose
       Rational a = new Rational(1,-2);
       Rational b = new Rational(3,4);
       System.out.println("a = "+a+" , b = "+b);
       a.add(b);
       System.out.println("a + b = "+a);
       System.out.println("a = "+a+" , b = "+b);
       a.substract(b);
       System.out.println("a - b = "+a);
       System.out.println("a = "+a+" , b = "+b);
       a.multiply(b);
       System.out.println("a * b = "+a);
       System.out.println("a = "+a+" , b = "+b);
       a.divide(b);
       System.out.println("a / b = "+a);
   }
}


OUTPUT :

a = -1/2 , b = 3/4
a + b = 1/4
a = 1/4 , b = 3/4
a - b = -1/2
a = -1/2 , b = 3/4
a * b = -3/8
a = -3/8 , b = 3/4
a / b = -1/2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote