Create a class called Rational for performing arithmetic with fractions. Write a driver program to test your class.
Use integer variables to represent the private data of the class—the numerator and the denominator. Provide a constructor function that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form
Provide public member functions for each of the
following:
a) Addition of two Rational numbers. The result should be stored in reduced form.
b) Subtraction of two Rational numbers. The result should be stored in reduced form.
c) Multiplication of two Rational numbers. The result should be stored in reduced form.
d) Division of two Rational numbers. The result should be stored in reduced form.
e) Printing Rational numbers in the form a/b where a is the numerator and b is the denominator. in java
import java.util.Scanner;
class Rational {
private int num; // the numerator
private int den; // the denominator
// create and initialize a new Rational object
public Rational(int numerator, int denominator) {
if (denominator == 0) {
throw new RuntimeException("Denominator is zero");
}
int g = gcd(numerator, denominator);
num = numerator / g;
den = denominator / g;
}
public String toString() {
if (den == 1)
return num + "";
else
return num + "/" + den;
}
// return (this * b)
public Rational multiplication(Rational b) {
return new Rational(this.num * b.num, this.den * b.den);
}
// return (this + b)
public Rational addition(Rational b) {
int numerator = (this.num * b.den) + (this.den * b.num);
int denominator = this.den * b.den;
return new Rational(numerator, denominator);
}
// return (this - b)
public Rational subtraction(Rational b) {
int numerator = (this.num * b.den) - (this.den * b.num);
int denominator = this.den * b.den;
return new Rational(numerator, denominator);
}
// return (1 / this)
public Rational reciprocal() {
return new Rational(den, num);
}
// return (this / b)
public Rational division(Rational b) {
return this.multiplication(b.reciprocal());
}
// return gcd(m, n)
private static int gcd(int m, int n) {
if (0 == n)
return m;
else
return gcd(n, m % n);
}
}
class App {
public static void main(String[] args) {
Rational x, y, z;
// 1/2 + 1/3 = 5/6
x = new Rational(1, 2);
y = new Rational(1, 3);
z = x.addition(y);
System.out.println("1/2 + 1/3 = " + z);
// 8/9 + 1/9 = 1
x = new Rational(8, 9);
y = new Rational(1, 9);
z = x.addition(y);
System.out.println("8/9 + 1/9 = " + z);
// 8/9 - 1/9 = 7/9
x = new Rational(8, 9);
y = new Rational(1, 9);
z = x.subtraction(y);
System.out.println("8/9 - 1/9 = " + z);
// 4/17 * 7/3 = 28/51
x = new Rational(4, 17);
y = new Rational(7, 3);
z = x.multiplication(y);
System.out.println("4/17 * 7/3 = " + z);
// 203/16957 * 9299/5887 = 17/899
x = new Rational(203, 16957);
y = new Rational(9299, 5887);
z = x.multiplication(y);
System.out.println("203/16957 * 9299/5887 = " + z);
// 0/6 = 0
x = new Rational(0, 6);
System.out.println("0/6 = " + x);
}
}
Comments
Leave a comment