Write a class encapsulating the concept of a rational number
Write a class encapsulating the concept of a rational number, assuming a rational number has the following attributes: an integer representing the numerator of the rational number another integer representing the denominator of the rational number Include a constructor, the accessors and mutators, and methods toString and equals. You should not allow the denominator to equal to 0; you should give it the default value 1 in case the corresponding argument of the constructor or a method is 0. Also include methods performing multiplication of a rational number by another and addition of a rational number to another, returning the resulting rational number in both cases. Write a client class to test all the methods in your class.
Sample ouput:
The numerator of rational #1 is 2
The denominator of rational #1 is 3
Rational #2 is 5/7
2/3 * 5/7 = 10/21
2/3 + 5/7 = 29/21
Original rational #1 and #2 are different
Original rational #1 and modified rational #2 are identical
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
############ Rational.java ################
public class Rational {
/**
* instance variable
*/
public final int numerator;
/**
* instance variable
*/
public final int denominator;
/**
* param n
* param d
*/
public Rational(int n, int d) {
if(d == 0){
System.out.println(\"Denominator can not be negative\");
System.exit(0);
}
// finding gcd of n and d
int gcd = reduce(n, d);
// storing numerator and denominator in reduced form
numerator = n/gcd;
denominator = d/gcd;
}
// toString method
public String toString(){
return numerator+\"/\"+denominator;
}
// equal method
public boolean equals(Object other)
{
if( other != null && ! (other instanceof Rational ) ) return false;
Rational that = (Rational)other;
if(numerator == that.numerator && denominator == that.denominator)
return true;
else
return false;
}
/**
* function to calculate gcd of numerator and denominator
*/
private int reduce(int numerator, int denominator) {
int min = numerator;
int gcd;
if (numerator > denominator) {
min = denominator;
}
for (gcd = min; gcd > 1; gcd--) {
if (numerator % gcd == 0 && denominator % gcd == 0) {
break;
}
}
return gcd;
}
public Rational add(Rational other){
int num = numerator*other.denominator + denominator*other.numerator;
int deno = numerator*denominator;
int gcd = reduce(num, deno);
return new Rational(num/gcd, deno/gcd);
}
public Rational multiply(Rational other){
int num = numerator*other.numerator;
int deno = denominator*other.denominator;
int gcd = reduce(num, deno);
return new Rational(num/gcd, deno/gcd);
}
}
############## RationalDriver.java ###############
public class RationalDriver {
public static void main(String[] args) {
Rational a = new Rational(1,2);
Rational b = new Rational(3,4);
System.out.println(\"a:\"+a.toString());
System.out.println(\"b:\"+b.toString());
a.add(b);
Rational c = a.add(b);
Rational d = a.multiply(b);
System.out.println(\"a:\"+c.toString());
System.out.println(\"b:\"+b.toString());
System.out.println(\"d:\"+d.toString());
System.out.println(\"a.equals(b):\" + a.equals(b));
}
}
/*
Sample output:
a:1/2
b:3/4
a:5/1
b:3/4
d:3/8
a.equals(b):false
*/



