In: Computer Science
Must be coded in C#.
10.8 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write an app 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 parameterless constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations (all calculation results should be stored in a reduced form): a) Add two Rational numbers. b) Subtract two Rational numbers. c) Multiply two Rational numbers. d) Divide two Rational numbers. e) Display Rational numbers in the form a/b, where a is the numerator and b is the denominator. f) Display Rational numbers 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.
Hi. I have answered similar questions before. Here is the completed code for this problem including the code for testing. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
using System;
public class Rational {
private
int numerator;
private
int denominator;
public
Rational(int numerator,
int denominator) {
if
(denominator == 0) {
/**
*
if denominator is 0, throwing an exception.
*/
throw
new
InvalidOperationException("Denominator can't be
0");
}
/**
* Finding the
GCD
*/
int
gcd = Greatestcommondivisor(numerator,
denominator);
/**
* removing
the GCD from numer..and denom..
*/
numerator =
numerator / gcd;
denominator =
denominator / gcd;
/**
* setting
values to the variables
*/
this.numerator
= numerator;
this.denominator
= denominator;
//
normalizing
Normalize();
}
public
Rational(int
numerator):this(numerator, 1) {
}
public
Rational() :this(0, 1){
}
/**
* A private method to calculate the
greatest common divisor of two numbers
*/
private
int
Greatestcommondivisor(int x,
int y) {
if
(y == 0)
return
x;
return
Greatestcommondivisor(y, x % y);
}
/**
* method to add a rational number to
the current number and returns it
*/
public Rational
Add(Rational b) {
int
num = (this.numerator * b.denominator)
+
(this.denominator * b.numerator);
int
den = this.denominator * b.denominator;
Rational rational =
new Rational(num, den);
return
rational;
}
/**
* method to subtract a rational
number from the current number and returns
* it
*/
public Rational
Sub(Rational b) {
int
num = (this.numerator * b.denominator)
-
(this.denominator * b.numerator);
int
den = this.denominator * b.denominator;
Rational rational =
new Rational(num, den);
return
rational;
}
/**
* method to multiply a rational
number to the current number and returns it
*/
public Rational
Mul(Rational b) {
int
num = this.numerator * b.numerator;
int
den = this.denominator * b.denominator;
Rational rational =
new Rational(num, den);
return
rational;
}
/**
* method to divide a rational number
from the current number and returns it
*/
public Rational
Div(Rational b) {
/**
* Finding the
recipocal of second number and multiplying it
*/
b =
b.Reciprocal();
return
Mul(b);
}
/**
* method to return the reciprocal of
a rational number
*/
public Rational
Reciprocal() {
int
newNumerator = this.denominator;
int
newDenominator =
Math.Abs(this.numerator);
if
(this.numerator < 0) {
newNumerator
= 0 - newNumerator;
}
return
new Rational(newNumerator,
newDenominator);
}
public void
Normalize() {
/**
* if denom is
less than 0, making it positive and changing the sign of
*
numerator
*/
if
(denominator < 0) {
//
multiplying numerator and denominator with -1 to swap signs
denominator
*= -1;
numerator
*= -1;
}
}
/**
* method to return a string
representation of a number
*/
public override String
ToString() {
return numerator +
"/" + denominator;
}
/**
* method to display rational number
in a/b form
*/
public void
DisplayRational(){
Console.WriteLine(ToString());
}
/**
* method to display the rational
number in fractional form
* allowing user to choose precision,
or using a default value 2
*/
public void
DisplayAsFraction(int
precision=2){
double
fraction=(double)numerator/denominator;
Console.WriteLine(Math.Round(fraction,precision));
}
}
//class to test the Rational class and methods
public class RationalTest {
public static void
Main(String[] args) {
//creating two
rational numbers and testing the methods
Rational a =
new Rational(2, 3);
Rational b =
new Rational(1, 2);
Console.WriteLine(a
+ " + " + b + " = " + a.Add(b));
Console.WriteLine(a
+ " - " + b + " = " + a.Sub(b));
Console.WriteLine(a
+ " * " + b + " = " + a.Mul(b));
Console.WriteLine(a
+ " / " + b + " = " + a.Div(b));
Console.Write(a+"
as Fraction: ");
//displaying a as
fraction with a precision of 4 digits after decimal point
a.DisplayAsFraction(4);
//waiting for a key
press to quit, remove if not necessary.
Console.WriteLine("\n\n\nPress
any key to quit");
Console.ReadKey();
}
}
/*OUTPUT*/
2/3 + 1/2 = 7/6
2/3 - 1/2 = 1/6
2/3 * 1/2 = 1/3
2/3 / 1/2 = 4/3
2/3 as Fraction: 0.6667