8.15 (Rational Numbers) Create a class called Rational for performing arithmetic
ID: 3878038 • Letter: 8
Question
8.15 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations:
a) Add two Rational numbers: The result of the addition should be stored in reduced form. Implement this as a static method.
b) Subtract two Rational numbers: The result of the subtraction should be stored in reduced form. Implement this as a static method.
c) Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. Implement this as a static method.
d) Divide two Rational numbers: The result of the division should be stored in reduced form. Implement this as a static method.
e) Return a String representation of a Rational number in the form a/b, where a is the numerator and b is the denominator .
f) Return a String representation of a Rational number in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.)
*Your program should implement exception handling if needed.
The program must be written in java language
Explanation / Answer
Rational.java
import java.text.DecimalFormat;
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 adding two rational numbers
public static Rational add(Rational r1, Rational r2) {
int num1 = r1.getNumerator();
int denom1 = r1.getDenominator();
int num2 = r2.getNumerator();
int denom2 = r2.getDenominator();
int num3 = (num1 * denom2) + (num2 * denom1);
int denom3 = denom1 * denom2;
Rational res = new Rational(num3, denom3);
return res;
}
//Method which adding two rational numbers
public static Rational subtract(Rational r1, Rational r2) {
int num1 = r1.getNumerator();
int denom1 = r1.getDenominator();
int num2 = r2.getNumerator();
int denom2 = r2.getDenominator();
int num3 = (num1 * denom2) - (num2 * denom1);
int denom3 = denom1 * denom2;
Rational res = new Rational(num3, denom3);
return res;
}
//Method which adding two rational numbers
public static Rational multiply(Rational r1, Rational r2) {
int num1 = r1.getNumerator();
int denom1 = r1.getDenominator();
int num2 = r2.getNumerator();
int denom2 = r2.getDenominator();
int num3 = (num1 * num2);
int denom3 = denom1 * denom2;
Rational res = new Rational(num3, denom3);
return res;
}
//Method which adding two rational numbers
public static Rational divide(Rational r1, Rational r2) {
int num1 = r1.getNumerator();
int denom1 = r1.getDenominator();
int num2 = r2.getNumerator();
int denom2 = r2.getDenominator();
int num3 = (num1 * denom2);
int denom3 = (num2 * denom1);
Rational res = new Rational(num3, denom3);
return res;
}
//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);
}
}
//Displaying the RationalNumber in Decimal Format
public String toDecimal() {
// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat("00.00");
return df.format(((float)(numerator) / denominator));
}
}
________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = 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 r1 = new Rational(num1, denom1);
Rational r2 = new Rational(num2, denom2);
System.out.println(" ");
// Reducing the Rational Numbers
System.out.println("** Rational Reduced Forms **");
System.out.println("(" + num1 + "/" + denom1 + ") reduced to :" + r1.toString());
System.out.println("(" + num2 + "/" + denom2 + ") reduced to :" + r2.toString());
System.out.println("Rational Number #1 in Decimal Form :" + r1.toDecimal());
System.out.println("Rational Number #2 in Decimal Form :" + r2.toDecimal());
System.out.println(" ** Adding two Rational Numbers **");
// Addition of two Rational Numbers
Rational r11 = Rational.add(r1, r2);
System.out.println("(" + r1.toString() + ") + " + "(" + r2.toString() + ") = " + r11.toString());
System.out.println(" ** Subtracting two Rational Numbers **");
Rational r12 = Rational.subtract(r1, r2);
System.out.println("(" + r1.toString() + ") - " + "(" + r2.toString() + ") = " + r12.toString());
System.out.println(" ** Multiplying two Rational Numbers **");
Rational r13 = Rational.multiply(r1, r2);
System.out.println("(" + r1.toString() + ") * " + "(" + r2.toString() + ") = " + r13.toString());
System.out.println(" ** Dividing two Rational Numbers **");
Rational r14 = Rational.divide(r1, r2);
System.out.println("(" + r1.toString() + ") / " + "(" + r2.toString() + ") = " + r14.toString());
System.out.println(" ** Comparing two Rational Numbers **");
System.out.print("Do you want to continue(Y/N):");
char c = sc.next(".").charAt(0);
if (c == 'Y' || c == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}
__________________
Output:
:: Enter Rational No 1 ::
Enter Numerator =4
Enter Denominator =8
:: Enter Rational No 2 ::
Enter Numerator =3
Enter Denominator =27
** Rational Reduced Forms **
(4/8) reduced to :1 / 2
(3/27) reduced to :1 / 9
Rational Number #1 in Decimal Form :00.50
Rational Number #2 in Decimal Form :00.11
** Adding two Rational Numbers **
(1 / 2) + (1 / 9) = 11 / 18
** Subtracting two Rational Numbers **
(1 / 2) - (1 / 9) = 7 / 18
** Multiplying two Rational Numbers **
(1 / 2) * (1 / 9) = 1 / 18
** Dividing two Rational Numbers **
(1 / 2) / (1 / 9) = 9 / 2
** Comparing two Rational Numbers **
Do you want to continue(Y/N):n
:: Program Exit ::
_______________Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.