Pages

Quadratic Equations : Java : BlueJ

Objective :

The BlueJ Program of class Quad calculates the Type Of Root(s) of the Quadratic Equation and Displays the Program by Proper Corresponding Functions.

In mathematics, a quadratic equation is a uni variate polynomial equation of the second degree. A general quadratic equation can be written in the form where x represents a variable or an unknown, and a, b, and c are constants with a ≠ 0. (If a = 0, the equation is a linear equation.) The constants a, b, and c are called respectively, the quadratic coefficient, the linear coefficient and the constant term or free term. The term "quadratic" comes from quadratus, which is the Latin word for "square". Quadratic equations can be solved by factoring, completing the square, graphing, Newton's method, and using the quadratic formula.
The roots are given by the quadratic formula
$x=\frac{-b \pm \sqrt {b^2-4ac}}{2a},$
where the symbol "±" indicates that both
$x=\frac{-b + \sqrt {b^2-4ac}}{2a}\quad\text{and}\quad x=\frac{-b - \sqrt {b^2-4ac}}{2a}$
are solutions of the quadratic equation.

A quadratic equation with real coefficients can have either one or two distinct real roots, or two distinct complex roots. In this case the discriminant determines the number and nature of the roots. There are three cases:
• If the discriminant is positive, then there are two distinct roots, both of which are real numbers:
$\frac{-b + \sqrt {\Delta}}{2a} \quad\text{and}\quad \frac{-b - \sqrt {\Delta}}{2a}$
For quadratic equations with integer coefficients, if the discriminant is a perfect square, then the roots are rational numbers—in other cases they may be quadratic irrationals.
• If the discriminant is zero, then there is exactly one distinct real root, sometimes called a double root:
$-\frac{b}{2a} . \,\!$
• If the discriminant is negative, then there are no real roots. Rather, there are two distinct (non-real) complex roots, which are complex conjugates of each other:
$\frac{-b}{2a} + i \frac{\sqrt {-\Delta}}{2a}, \quad\text{and}\quad \frac{-b}{2a} - i \frac{\sqrt {-\Delta}}{2a},$
where i is the imaginary unit.
Thus the roots are distinct if and only if the discriminant is non-zero, and the roots are real if and only if the discriminant is non-negative.

BlueJ Program Screenshot :

Java Program Source Code :

/**
* The BlueJ Program of class Quad calculates the Type Of Root(s) of the
* Quadratic Equation and Displays the Program by Proper Corresponding Functions.
* @author SHANTANU KHAN
* @mail shantanukhan1995@gmail.com
* @website 0code.blogspot.com
* Program Type : BlueJ Program - Java
*/
import java.util.*;
{
static Scanner sc=new Scanner(System.in);
public double a,b,c,X1,X2;

public void input()
{
System.out.println("Enter values of Quadratic Equation : ax2+bx+c=0");
System.out.print("a : ");a=sc.nextDouble();
System.out.print("b : ");b=sc.nextDouble();
System.out.print("c : ");c=sc.nextDouble();
}

public double discriminant() // CALCULATING DISCRIMINANT
{
return (Math.pow(b,2)-(4*a*c));
}
public void real() // FOR REAL ROOTS
{
double real1=((-b+Math.sqrt(b*b-4*a*c))/2.0*a);
double real2=((-b-Math.sqrt(b*b-4*a*c))/2.0*a);
System.out.println("1st Real Root : "+real1);
System.out.println("2nd Real Root : "+real2);
}
public void equal() // FOR REAL AND EQUAL ROOTS
{
double equal=((-b/(2.0*a)));
System.out.println("Equal Real Root : "+equal);
}
public void imaginary() // FOR IMAGINARY ROOTS
{
double imaginaryPart=(Math.sqrt(Math.abs(b*b-4.0*a*c)));
double realPart=(-b/(2.0*a));
System.out.println(realPart+" + "+imaginaryPart+" i");
System.out.println(realPart+" - "+imaginaryPart+" i");
}

public void root() // CONDITIONAL FUNCTION EXECUTION
{
if(discriminant()==0){
System.out.println("\nBoth the Roots are Equal:"); equal(); }
else if(discriminant()<0){
System.out.println("\nThe Roots are Imaginary :"); imaginary(); }
else{
System.out.println("The Roots are Real and Unequal :"); real(); }
}

public static void main(String[] args)
{
}