Answer to Question #270771 in C++ for pica

Question #270771
  1. (Quadratic Equation) Design a class named QuadraticEquation for a quadratic equation ax2 + bx + c = 0. The class contains:  
  • Data fields a, b, and c that represent three coefficients.
  • A constructor for the arguments for a, b, and c.
  • Three get functions for a, b, and c.
  • A function named getDiscriminant() that returns the discriminant, which is b2 - 4ac.
  • The functions named getRoot1() and getRoot2() for returning two roots of the equation:

          x1 = -b + sqrt(b2-4ac) / 2a

x2 = -b - sqrt(b2-4ac) / 2a

These functions are useful only if the discriminant is non-negative. Let these functions

return 0 if the discriminant is negative.

Write a test program that prompts the user to enter values for a, b, and c, and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display "The equation has no real roots".


1
Expert's answer
2021-11-25T02:32:40-0500


#include <iostream>
#include <math.h>
using namespace std;


class QuadraticEquation{
private:
	//Data fields a, b, and c that represent three coefficients.
	float a,b,c;


public:
	QuadraticEquation(){}
	//A constructor for the arguments for a, b, and c.
	QuadraticEquation(float a,float b,float c){
		this->a=a;
		this->b=b;
		this->c=c;
	}
	//Three get functions for a, b, and c.
	float getA(){
		return this->a;
	}
	float getB(){
		return this->b;
	}
	float getC(){
		return this->c;
	}
	//A function named getDiscriminant() that returns the discriminant, which is b2 - 4ac.
	float getDiscriminant(){
		return b*b-4*a*c;
	}
	//The functions named getRoot1() and getRoot2() for returning two roots of the equation:
	//These functions are useful only if the discriminant is non-negative. 
	//Let these functions return 0 if the discriminant is negative.
	float getRoot1(){
		float D=getDiscriminant();
		if(D<0){
			return 0;
		}
		return ((-1)*b+sqrt(D))/(2*a);
	}
	float getRoot2(){
		float D=getDiscriminant();
		if(D<0){
			return 0;
		}
		return ((-1)*b-sqrt(D))/(2*a);
	}
};


int main()
{
	float a,b,c;
	
	//Write a test program that prompts the user to enter values for a, b, and c, and 
	//displays the result based on the discriminant. 
	cout<<"Enter a: ";
	cin>>a;
	cout<<"Enter b: ";
	cin>>b;
	cout<<"Enter c: ";
	cin>>c;
	QuadraticEquation quadraticEquation(a,b,c);
	//If the discriminant is positive, display the two roots. 
	if(quadraticEquation.getDiscriminant()>0){
		cout<<"Root 1 = "<<quadraticEquation.getRoot1()<<"\n";
		cout<<"Root 2 = "<<quadraticEquation.getRoot2()<<"\n";
	}else if(quadraticEquation.getDiscriminant()==0){
		//If the discriminant is 0, display the one root. 
		cout<<"Root = "<<quadraticEquation.getRoot1()<<"\n";
	}else{
		//Otherwise, display "The equation has no real roots".
		cout<<"The equation has no real roots\n\n";
	}
	
	cin>>a;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS