Question: Using C# Rational.cs represents a fraction and we are going to use operator overloading to add, subtract, multiply, and divide rational numbers. You will need

Using C#

Rational.cs represents a fraction and we are going to use operator overloading to add, subtract, multiply, and divide rational numbers. You will need to complete the following methods, compareTo, equals, toString, subtract operator, and the divide operator. I have provided examples for the add and multiply operators. Finally complete the method RationalToDecimal which should show the rational number is decimal form. Make sure that all of your rational numbers are reduced correctly so if I were to create a rational number and pass in (2,4) your explicit value constructor should reduce the number accordingly and when toString is called it should show (1/2). Please see comments within each method for implementation specifics. Keep in mind that you cannot change the values of the rational number once the object is created so make sure you use the explicit value constructor.

Provided Code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace Rational { public class Rational : IComparable { private int Numerator { get; } private int Denominator { get; }

public Rational() { //default constructor Numerator = 0; Denominator = 0; } public Rational(int numerator, int denominator) { //explicit value constructor that needs to be modified to reduce the fraction into smallest form so 2/4 must be reduced to 1/2 this.Numerator = numerator; this.Denominator = denominator; }

public int CompareTo(object obj) { //compare two rational numbers and return an integer showing how they compare, it is up to you how you are going to handle the conversion of your number to int form throw new NotImplementedException(); }

public override bool Equals(object obj) { //implement the standard equals method throw new NotImplementedException(); }

public override string ToString() { //string must return the fraction in the form a/b and in reduced form return base.ToString(); }

public static Rational operator +(Rational a, Rational b) { return new Rational(a.Numerator * b.Denominator + b.Numerator * a.Denominator, a.Denominator * b.Denominator); } public static Rational operator *(Rational a, Rational b) { return new Rational(a.Numerator * b.Numerator, a.Denominator * b.Denominator); }

public static Rational operator -(Rational a, Rational b) { throw new NotImplementedException(); }

public static Rational operator /(Rational a, Rational b) { throw new NotImplementedException(); }

public string RationalToDecimal() { throw new NotImplementedException(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!