0% found this document useful (0 votes)
4 views1 page

OOP Programming Problem Final

The document outlines the creation of a Quadratic class to represent a quadratic expression of the form ax² + bx + c, including methods for evaluating the expression, calculating the discriminant, and determining the nature of the roots. It also specifies methods for finding the roots and checking if they are real or imaginary, as well as if they form a perfect square. Additionally, sample main program runs demonstrate user input and expected output for various test cases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

OOP Programming Problem Final

The document outlines the creation of a Quadratic class to represent a quadratic expression of the form ax² + bx + c, including methods for evaluating the expression, calculating the discriminant, and determining the nature of the roots. It also specifies methods for finding the roots and checking if they are real or imaginary, as well as if they form a perfect square. Additionally, sample main program runs demonstrate user input and expected output for various test cases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Create a class called Quadratic for representing a one-variable quadratic expression of the form:

ax2 + bx + c
a,b and c here are the coefficients.

The class should contain the following methods:


• A constructor that accepts values for a, b, and c.
• method getA()
• method getB()
• method getC()
• method evaluate (x) -- int
-will return the value of the expression at point x
• method discriminant()
- that will return (b2 – 4ac)
• method boolean isImaginaryRoots()
-roots are imaginary if (b2 – 4ac) < 0
• method boolean isRealRoots()
-roots are real if (b2 – 4ac) >= 0

// these methods can only be invoked if the roots are not imaginary
• method firstRoot() --> float

− b + b 2 − 4ac
x1 =
2a
• method secondRoot() --> float

− b − b 2 − 4ac
x2 =
2a
• method isPerfectSquare() --> boolean
// If the first and second roots are equal

Write a sample main program that will work as shown below.

Example run of the program: (Test Case No. 1)

Enter coefficient a: 4
Enter coefficient b: 4 Input from the user
Enter coefficient c: 1

Quadratic expression: 4x2 + 4x + 1


The roots are real: x1 = -0.5 ; x2 = -0.5 Output
It is a perfect square.

Evaluating the expression:


Enter x: 2 Input from the user
Result : 25 Output

Example run of the program: (Test Case No. 2)

Enter coefficient a: 2
Enter coefficient b: 5 Input from the user
Enter coefficient c: 10

Quadratic expression: 2x2 + 5x + 10 Output


The roots are imaginary.

Evaluating the expression:


Enter x: 1 Input from the user
Result : 17 Output
Example run of the program: Test Case No. 3)

Enter coefficient a: 1
Enter coefficient b: 0 Input from the user
Enter coefficient c: -1

Quadratic expression: x2 + 0x + -1
The roots are real. x1 = 1 ; x = -1 Output
It is not a perfect square.

Evaluating the expression:


Enter x: 5 Input from the user
Result : 24 Output

You might also like