C# (In Console App) - Homework Help The method DoMathProblem has 1 parameter: a
ID: 3880620 • Letter: C
Question
C# (In Console App) - Homework Help
The method DoMathProblem has 1 parameter: a MathDelegate type. What specifically can be passed to this method as an argument (see code below)?
For instance, if a method has an integer as its parameter, you can pass any expression that evaluates (or simplifies) to an integer number as an argument.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment1c
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter first operand: ");
double num1 = double.Parse(Console.ReadLine());
Console.Write("Enter second operand: ");
double num2 = double.Parse(Console.ReadLine());
GenericMath genMath = new GenericMath() { Operand1 = num1, Operand2 = num2 };
double sum = genMath.DoMathProblem((o1, o2) => o1 + o2);
Console.WriteLine($"The sum is {sum}");
double diff = genMath.DoMathProblem((o1, o2) => o1 - o2);
Console.WriteLine($"The difference is {diff}");
double mult = genMath.DoMathProblem((o1, o2) => o1 * o2);
Console.WriteLine($"The product is {mult}");
double div = genMath.DoMathProblem((o1, o2) =>
{
if (o2 != 0)
return o1 / o2;
else
{
throw new DivideByZeroException();
}
});
Console.WriteLine($"The quotient is {div}");
#if DEBUG
Console.WriteLine("Press enter to close...");
Console.ReadLine();
#endif
}
/*static double Add(double operand1, double operand2)
{
return operand1 + operand2;
}*/
/*static double Subtract(double operand1, double operand2)
{
return operand1 - operand2;
}*/
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment1c
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter first operand: ");
int num1 = Convert.Int32(Console.ReadLine());
Console.Write("Enter second operand: ");
int num2 = convert.Int32(Console.ReadLine());
GenericMath genMath = new GenericMath() { Operand1 = num1 };
int sum = genMath.Add(o1) ;
Console.WriteLine($"The sum is {sum}");
int diff = genMath.subtract(o1);
Console.WriteLine($"The difference is {diff}");
int mult = genMath.mul(o1);
Console.WriteLine($"The product is {mult}");
Console.Write("Enter second operand: ");
int operand2 = convert.Int32(Console.ReadLine())
int div = genMath.DoMathProblem((o1, o2) =>
{
if (o2 != 0)
return o1 / o2;
else
{
throw new DivideByZeroException();
}
});
Console.WriteLine($"The quotient is {div}");
#if DEBUG
Console.WriteLine("Press enter to close...");
Console.ReadLine();
#endif
}
// Sum of two numbers
static int Add(int operand1)
{
Console.Write("Enter second operand: ");
int operand2 = convert.Int32(Console.ReadLine());
return operand1 + operand2;
}
// Difference of two Numbers
static int subtract(int operand1)
{
Console.Write("Enter second operand: ");
int operand2 = convert.Int32(Console.ReadLine());
return operand1 - operand2;
}
// Multification of two Numbers
static int mul(int operand1)
{
Console.Write("Enter second operand: ");
int operand2 = convert.Int32(Console.ReadLine());
return operand1 * operand2;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.