# Java program to get real solutions of quadratic equation

Write a Java program that prints all real solutions to the quadratic equation ax^2 + bx + c = 0. Read in a, b, c and use the quadratic formula. If the discriminant (b^2 - 4ac) is negative, display a message stating that there are no real solutions.

Implement a class QuadraticEquation whose constructor receives the coefficients a, b, c of the quadratic equation. Supply methods getSolution1 and getSolution2 that get the solutions, using the quadratic formula, or 0 if no solution exists. The getSolution1 method should return the smaller of the two solutions.

Supply a boolean method hasSolutions that returns false if the discriminant is negative.

Use the following class as your tester class:

/**

This program tests the QuadraticEquation class.

*/

public class QuadraticEquationTester

{

public static void main(String[] args)

{

QuadraticEquation eq1 = new QuadraticEquation(2, 2, -4);

System.out.println(eq1.hasSolutions());

System.out.println("Expected: true");

System.out.println(eq1.getSolution1());

System.out.println("Expected: -2");

System.out.println(eq1.getSolution2());

System.out.println("Expected: 1");

QuadraticEquation eq2 = new QuadraticEquation(-2, -2, 4);

System.out.println(eq2.hasSolutions());

System.out.println("Expected: true");

System.out.println(eq2.getSolution1());

System.out.println("Expected: -2");

System.out.println(eq2.getSolution2());

System.out.println("Expected: 1");

QuadraticEquation eq3 = new QuadraticEquation(2, 2, 4);

System.out.println(eq3.hasSolutions());

System.out.println("Expected: false");

System.out.println(eq3.getSolution1());

System.out.println("Expected: 0");

System.out.println(eq3.getSolution2());

System.out.println("Expected: 0");

}

}

https://brainmass.com/computer-science/java/java-program-to-get-real-solutions-of-quadratic-equation-460732

#### Solution Preview

Please rename attached 460732_QuadraticEquationTester.java as QuadraticEquationTester.java before you compile it.

Required QuadraticEquation class has been implemented in the same file.

class QuadraticEquation

{

private int a, b, c, discriminant;

QuadraticEquation (int a, int b, int c)

{

this.a = a;

this.b = b;

this.c = c;

discriminant = (b*b) - 4*a*c;

}

boolean hasSolutions ()

{

if (discriminant < 0)

{

System.out.println("There are no real solutions.");

return false;

}

else

...

#### Solution Summary

Please rename attached 460732_QuadraticEquationTester.java as QuadraticEquationTester.java before you compile it. Required QuadraticEquation class has been implemented in the same file.