Using C# I need a console application class that will allow for the addition, su
ID: 3696680 • Letter: U
Question
Using C#
I need a console application class that will allow for the addition, subtraction, multiplication and division of 2 rational numbers. The class may use operator overloading for +, -, * and / operations. The class must have ToString and Equals methods that override the ToString and Equal methods of the object class. The rational number must be properly formatted and in simplest form. Improper fractions are allowed(11/98, 121/25 ). You may use the following as a starting point for your class:
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Binary_operator_overload_cs
{
class arOP
{
public double a, result;
public void set(double x)
{
a = x;
}
public static arOP operator +(arOP o4, arOP o5)
{
arOP o6 = new arOP();
o6.result = o4.a + o5.a;
return o6;
}
public static arOP operator -(arOP o4, arOP o5)
{
arOP o6 = new arOP();
o6.result = o4.a - o5.a;
return o6;
}
public static arOP operator *(arOP o4, arOP o5)
{
arOP o6 = new arOP();
o6.result = o4.a * o5.a;
return o6;
}
public static arOP operator /(arOP o4, arOP o5)
{
arOP o6 = new arOP();
o6.result = o4.a / o5.a;
return o6;
}
}
class Program
{
static void Main(string[] args)
{
arOP o1 = new arOP();
arOP o2 = new arOP();
arOP o3 = new arOP();
o1.set(20);
o2.set(30);
o3 = o1 + o2;
Console.WriteLine("Addition is " + o3.result);
Console.ReadKey();
o3 = o1 - o2;
Console.WriteLine("Substraction is " + o3.result);
Console.ReadKey();
o3 = o1 * o2;
Console.WriteLine("Multiply is " + o3.result);
Console.ReadKey();
o3 = o1 / o2;
Console.WriteLine("Division is " + o3.result);
Console.ReadKey();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.