Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in c# convert the code below that uses Rational Methods to use Overloaded Operators. using System; /* 10.8 (Rational Numbers) Create a class called Rational

in c#

convert the code below that uses Rational Methods to use Overloaded Operators.

using System;

/* 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 classthe 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 */ namespace Exercise_10_08 { class Program { static void Main(string[] args) { Rational Fraction1 = new Rational(5, 10);//to change fraction do so here Rational Fraction2 = new Rational(5, 10); Console.WriteLine("Fraction1 = " + Fraction1); Console.WriteLine("Fraction2 = " + Fraction2); Console.WriteLine("Fraction1 + Fraction2 = " + (Fraction1 + Fraction2)); Console.WriteLine("Fraction1 - Fraction2 = " + (Fraction1 - Fraction2)); Console.WriteLine("Fraction1 * Fraction2 = " + (Fraction1 * Fraction2)); Console.WriteLine("Fraction1 / Fraction2 = " + (Fraction1 / Fraction2)); }

class Rational { private int top; private int bot; public Rational(int numerator, int denominator) { this.top = numerator; this.bot = denominator; Reduce(); } private void Reduce() { int gcd = Gcd(top, bot); top /= gcd; bot /= gcd; } private int Gcd(int a, int b) { if (b == 0) { return a; } else { return Gcd(b, a % b); } } public static Rational operator +(Rational Fraction1, Rational Fraction2) { int numerator = Fraction1.top * Fraction2.bot + Fraction1.bot * Fraction2.top; int denominator = Fraction1.bot * Fraction2.bot; return new Rational(numerator, denominator); } public static Rational operator -(Rational Fraction1, Rational Fraction2) { int numerator = Fraction1.top * Fraction2.bot - Fraction1.bot * Fraction2.top; int denominator = Fraction1.bot * Fraction2.bot; return new Rational(numerator, denominator); } public static Rational operator *(Rational Fraction1, Rational Fraction2) { int numerator = Fraction1.top * Fraction2.top; int denominator = Fraction1.bot * Fraction2.bot; return new Rational(numerator, denominator); } public static Rational operator /(Rational Fraction1, Rational Fraction2) { int numerator = Fraction1.top * Fraction2.bot; int denominator = Fraction1.bot * Fraction2.top; return new Rational(numerator, denominator); } public override string ToString() { if (bot == 1) { return top.ToString(); } else { return top + "/" + bot; } } } } }

Step by Step Solution

3.32 Rating (161 Votes )

There are 3 Steps involved in it

Step: 1

Modified code using System namespace Exercise1008 class Program static void Mainstring args Rational ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions

Question

Explain the statement, In the long run, there are no fixed costs.

Answered: 1 week ago