Using C# Rational.cs represents a fraction and we are going to use operator over
ID: 3748167 • Letter: U
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 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();
}
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Rational
{
int numerator;
public int Numerator
{
get { return numerator; }
set { numerator = value; }
}
int denominator;
public int Denominator
{
get { return denominator; }
set { denominator = value; }
}
public Rational(int num, int denum)
{
Numerator = num;
Denominator = denum;
}
public Rational()
{
//new Rational(1, 1);
Numerator = 1;
Denominator = 1;
}
public void RationalDisplay()
{
Console.WriteLine("{0} / {1}",Numerator,Denominator);
}
public Rational Topla (Rational addValue)
{
return new Rational(this.Denominator * addValue.Numerator + this.Numerator * addValue.Denominator, this.Denominator * addValue.Denominator);
}
public Rational Cikar(Rational addValue)
{
return new Rational(this.Numerator * addValue.Denominator - this.Denominator * addValue.Numerator, this.Numerator * addValue.Denominator);
}
public Rational Carp(Rational addValue)
{
return new Rational(this.Numerator * addValue.Numerator, this.Denominator * addValue.Denominator);
}
public Rational Bol(Rational addValue)
{
return new Rational(this.Numerator * addValue.Denominator, this.Denominator * addValue.Numerator);
}
private void Sadelestir()
{
int kucukeleman;
int ebob = 1;
if (Numerator < Denominator)
kucukeleman = Numerator;
else
kucukeleman = Denominator;
for (int i = kucukeleman; i > 1; i--)
{
if (this.Numerator % i == 0 && this.Denominator % i == 0)
{
ebob = i;
break;
}
}
this.Numerator = this.Numerator / ebob;
this.Denominator = this.Denominator / ebob;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.