Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

/*

* To change this template, choose Tools | Templates


* and open the template in the editor.
*/
package quadratic.equation;
import java.util.Scanner;
/**
*
* @author smoakrs
*/
public class QuadraticEquation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a value for A");
double a = input.nextDouble();
System.out.println("Enter a value for B");
double b = input.nextDouble();
System.out.println("Enter a value for C");
double c = input.nextDouble();
QuadEquation equation = new QuadEquation(a, b, c);
double discriminant = equation.Discriminant();
if (discriminant < 0) { System.out.println("There are no roots for this equ
ation");}
else if (discriminant == 0)
{System.out.println("The root is " + equation.Root1());}
else
{System.out.println("The roots, they are " + equation.Root1()
+ " and " + equation.Root2());
}}}
class QuadEquation{
private double a;
private double b;
private double c;
QuadEquation(double anew, double bnew, double cnew){
a=anew;
b=bnew;
c=cnew;
}
double getA() {
return a;}
double getB() {
return b;}
double getC() {
return c;}
double Discriminant() {
return (b*b) - (4*a*c);
}

double Root1() {
if (Discriminant()<0)
return 0;
else {return (-b + Math.sqrt(Discriminant())) / (2*a);}
}
double Root2() {
if (Discriminant() < 0)
return 0;
else {return (-b - Math.sqrt(Discriminant())) / (2*a);}
}
}

You might also like