C# programming
Create a class called QuadraticEq that has a custom constructor that takes in a, b and c as the quadratic equation coefficients. Implement also the following:
Please pay attention to encapsulation concerns. Use your created class in the following way inside a Main method to prove the correctness of your class.
QuadraticEq q = new QuadraticEq(2,4,2);
Console.WriteLine("d = {0}", q.Discriminant);
double[] results = q.GetRealRoots();
foreach (double x in results)
Console.WriteLine(x);
If your class is correct, adding the following line to the end of main should cause a compilation error:
q.Discriminant = 10;
Copy and paste the complete program here:
using System.IO;
using System;
class QuadraticEq
{
private double a, b,c;
public QuadraticEq(double a1, double b1, double c1){
a = a1;
b = b1;
C= c1;
public double Discriminant{
get{
return b*b-4*a* c;
}
}
public double[] GetRealRoots(){
double d = Discriminant;
double[] roots = null;
if(d == 0) {
roots = new double[1];
roots[0] = -b/(2*a);
}
else if(d > O){
roots = new double[2];
roots[0] = (-b+ Math.Sqrt(d))/(2*a);
roots[1] = (-b - Math.Sqrt(d))/(2*a);
}
else
roots = new double[0];
return roots;
}
static void Main()
{
QuadraticEq q = new QuadraticEq(2, 4, 2);
Console.WriteLine("d = {0}", q.Discriminant);
double[] results = q.GetRealRoots();
foreach(double x in results)
Console.WriteLine(x);
}
OUTPUT:
d=0
-1
Get Answers For Free
Most questions answered within 1 hours.