Question

C# programming Create a class called QuadraticEq that has a custom constructor that takes in a,...

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:

  • A readonly property double Discriminant that represents discriminant to the equation
  • A method double[] GetRealRoots() that returns an array of the real roots of the equation; size can be 2, 1 or 0

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:

Homework Answers

Answer #1


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


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C# programming Assume we have a student final exam score lookup table like this: John 40...
C# programming Assume we have a student final exam score lookup table like this: John 40 Henry 0 Mary 59 Hillary 39 Pikachu 100 Write a class that helps check if student passes or fails the course. If a student's score is greater than or equal to a parameter passScore, we will consider it a pass. If a student's name is NOT found in the table, we assume they have missed the exam and thus consider failed. The method should...
Play musical chords (JAVA PROGRAMMING) Notes and frequencies Every musical note has a name and a...
Play musical chords (JAVA PROGRAMMING) Notes and frequencies Every musical note has a name and a frequency. You are given a file where each line contains a note name, a tab character, and a frequence, which is a floating point value. For example, here are the first few lines from the file notes_frequencies.txt: A0 27.5 A#0 29.1353 B0 30.8677 C1 32.7032 C#1 34.6479 D1 36.7081 D#1 38.8909 E1 41.2035 F1 43.6536 Playing chords Write a program named PlayChords that first...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent...
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. • in the term 4X2, the coefficient is 4 and the exponent 2 • in -6X8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
C# Programming Using the Example provided in this Week Folder,Create a Console application that solve the...
C# Programming Using the Example provided in this Week Folder,Create a Console application that solve the following problem: The heating system in a school should be switched on if the average temperature is less than 17 degrees Celsius. The average temperature is found from the temperatures in the Math, English and IT departments. You are required to write a program that allows the user to input 3 temperatures. The program calculates and displays the average temperature and then displays "heating...
Write a class called Item that has a string field called name and a double field...
Write a class called Item that has a string field called name and a double field called price and an integer field called quantity. In main, create a bag of type Item and place several item objects in the bag. Then in main calculate the total price of items purchased. You may remove each item and add the price to a total variable in a loop. The loop should end when the bag is empty. Using bag algorithms public final...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT