Question

Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c 0. The class contains: • The data fields a, b, and

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import math
class QuadraticEquation:
def __init__(self, a, b, c): #Initializer method
self.a = a
self.b = b
self.c = c
def getDiscriminant(self):
return ((self.b)**2) - (4 * self.a * self.c)
def geta(self): #getter methods
return self.a
def getb(self):
return self.b
def getc(self):
return self.c

def getRoot1(self):
if self.getDiscriminant() < 0:
return None
else:
return ((-1*self.getb()) - math.sqrt(self.getDiscriminant())) / (2 * self.geta())
def getRoot2(self):
if self.getDiscriminant() < 0:
return None
else:
return ((-1*self.getb()) + math.sqrt(self.getDiscriminant())) / (2 * self.geta())

print('Enter values for a, b and c')
a, b, c= map(int, input().split())
qe = QuadraticEquation(a,b,c) # creating object of class
if(qe.getDiscriminant()<0):
print('The equation has no roots')
elif(qe.getDiscriminant() == 0):
print('One Common Root of equation='+str(qe.getRoot1()))
else: print('Roots of equation are:'+str(qe.getRoot1())+" and "+str(qe.getRoot2()))
  

Explanation: The program has a class named QuadraticEquation with an initializer method, 3 getter methods for coefficients, a getDiscriminant method for calculating discriminant and 2 root calculationg methods. The program first takes input a, b and c , the coefficient of equationa and then create an object(instnace) of class with them. It then calculates the discriminant and then according gives out the roots of the equation.

Output and Code Identation:  

import math class QuadraticEquation: Output def __init__(self, a, b, c): self.a = a Enter values for a, b and c self.b = b 2

Add a comment
Know the answer?
Add Answer to:
Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python. The two roots of a quadratic equation ax^2 + bx + c = 0...

    In Python. The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r2 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...

  • java code Design a class named QuadraticEquation for a quadratic equation with real coefficients ax? +...

    java code Design a class named QuadraticEquation for a quadratic equation with real coefficients ax? + bx + x = 0 where a = 0. The class contains: (a) Private data fields a, b, and c that represent three coefficients. (b) A constructor taking arguments for a, b, and c. (c) Getter and setter methods for each attribute field. (d) A method named getDiscriminant() that returns the discriminant, 62 - 4ac. (e) A method named hasReal Solution that determines If...

  • C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠...

    C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...

  • This is in python language Algebra: solve quadratic equations) The two roots of a quadratic equation,...

    This is in python language Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, 0, can be obtained using the following fomula: b 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. Write a program that prompts the user to enter values for a, b, and cand...

  • Java Programming Question 4 (10 points): Solutions for a quadratic equation ax(squared)+bx+c= 0. where a does...

    Java Programming Question 4 (10 points): Solutions for a quadratic equation ax(squared)+bx+c= 0. where a does not equal zero are as follows. r1=( −b+√b(squared)−4ac)/2a r2=(−b−√b(squared)−4ac)/2a if b(squared)−4ac <0, equation doesn’t have real roots. If it is 0 there is one root(r1=r2). Write a Java program to read a,b and c from keyboard and find the roots, if they exist. Note: You need to have a method that takes 3 real values as arguments

  • The roots of the quadratic equation ax2 + bx + c = 0, a following formula:...

    The roots of the quadratic equation ax2 + bx + c = 0, a following formula: 0 are given by the In this formula, the term i2 - 4ac is called the discriminant. If b4ac 0 then the equation has a single (repeated) root. If -4ac > 0, th equation complex roots. Write a program that prompts the user to input the value of a (the coefficient of ), b (the coefficient of x), and c (the n has two...

  • The two roots of the quadratic equation ax2 + bx + c = 0 can be...

    The two roots of the quadratic equation ax2 + bx + c = 0 can be found using the quadratic formula as -b+ v62 – 4ac . -b-v6² – 4ac 1 X1 = - and x2 = 2a 2a When b2 – 4ac < 0 this yields two complex roots - -b V4ac – 62 -b Vac – 6² x1 = = +. . 2a 2a i. and x2 = . za 2al Using the quadratic formula the roots of...

  • Pseudocode and PYTHON source code, thanks! Program 1: Design (pseudocode) and implement (source code) a class...

    Pseudocode and PYTHON source code, thanks! Program 1: Design (pseudocode) and implement (source code) a class (name it QuadraticEquation) that represents a quadratic equation of the form of ax2+ bx + x = 0. The class defines the following variables and methods: Private data field a, b, and c that represent three coefficients. A constructor for the arguments for a, b, and c. Three get methods for a, b, and c. Method getDiscriminant()returns the discriminant value, which is disc =...

  • please answer this question with python. Write a program to solve the quadratic equation ax^2 +...

    please answer this question with python. Write a program to solve the quadratic equation ax^2 + bx + c = 0 using the standard quadratic formula x = -b plusminus Squareroot b^2 - 4ac/2a or the alternative formula x = 2c/-b Squareroot b^2 - 4ac. Your program should accept values for the coefficients a, b, and c as input and produce the two roots of the equation as output. Your program should detect when the roots are imaginary, but need...

  • A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the...

    A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the above quadratic equation is computed using the following formula, root1 = (-b + sqrt(D))/ 2a root2 = (-b - sqrt(D))/2a Where D is the discriminant of the quadratic equation and is computed as, D = b^2 - 4ac Given the value of D, the roots of a quadratic equation can be categorized as follows, D > 0 : Two distinct real roots D =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT