Berikut adalah rumus ABC :
Rumus Diskriminan :
Hukum persamaan kuadrat
Untuk ax2 + bx + c = 0, dengan a ≠ 0,
1. Jika D = 0, Memiliki 2 akar berebeda dan keduanya real.
2. Jika D > 0, Memiliki 2 akar berebeda dan keduanya real.
3. Jika D < 0, Akar-akar persamaan imaginer .
Jika D = 0
Jika D > 0
Jika D < 0
Sintaks Program :
import java.util.Scanner;
public class SquareEquationToy {
private double a;
private double b;
private double c;
private double x1;
private double x2;
//private boolean fValid = false;
public SquareEquationToy (double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
calcX();
}
private double calcDiscriminant (){
return b*b - 4*a*c;
}
private double imajiner() {
return -b/(2*a);
}
private void calcX() {
double d = calcDiscriminant ();
if (d > 0) {
x1 = (-b + Math.sqrt(d))/(2*a);
x2 = (-b - Math.sqrt(d))/(2*a);
//fValid = true;
}
}
/*public boolean isValid() {
return fValid;
}*/
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getx1() {
return x1;
}
public double getx2() {
return x2;
}
static void test() {
System.out.println ();
System.out.println ("<==Program mengitung persamaan kuadrat==> ");
System.out.println ("===========================================");
Scanner angka = new Scanner(System.in);
System.out.print("Masukan nilai a : ");
double a = angka.nextDouble();
System.out.println();
System.out.print("Masukan nilai b : ");
double b = angka.nextDouble();
System.out.println();
System.out.print("Masukan nilai c : ");
double c = angka.nextDouble();
System.out.println();
SquareEquationToy s = new SquareEquationToy(a,b,c);
System.out.println("Nilai diskriminan : " +s.calcDiscriminant());
System.out.println();
if (s.calcDiscriminant() > 0){
System.out.println ("Persamaan "+ s.getA() + "X^2" + " + " + s.getB() + "X" + " + "+ s.getC()+ " Memiliki 2 akar berebeda dan keduanya real "+ s.getx1() +" atau "+ s.getx2());
}
else if (s.calcDiscriminant() < 0) {
System.out.println ("Persamaan " + s.getA() + "X^2" + " + " + s.getB() + "X" + " + "+ s.getC()+ " Akar-akar persamaan imaginer ");
}
else {
System.out.println ("Persamaan " + s.getA() + "X^2" + " + " + s.getB() + "X" + " + "+ s.getC()+ " Akar-akar persamaan nyata tetapi kembar, yaitu " + s.imajiner());
}
}
public static void main (String [] args) {
test();
}
}
public class SquareEquationToy {
private double a;
private double b;
private double c;
private double x1;
private double x2;
//private boolean fValid = false;
public SquareEquationToy (double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
calcX();
}
private double calcDiscriminant (){
return b*b - 4*a*c;
}
private double imajiner() {
return -b/(2*a);
}
private void calcX() {
double d = calcDiscriminant ();
if (d > 0) {
x1 = (-b + Math.sqrt(d))/(2*a);
x2 = (-b - Math.sqrt(d))/(2*a);
//fValid = true;
}
}
/*public boolean isValid() {
return fValid;
}*/
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getx1() {
return x1;
}
public double getx2() {
return x2;
}
static void test() {
System.out.println ();
System.out.println ("<==Program mengitung persamaan kuadrat==> ");
System.out.println ("===========================================");
Scanner angka = new Scanner(System.in);
System.out.print("Masukan nilai a : ");
double a = angka.nextDouble();
System.out.println();
System.out.print("Masukan nilai b : ");
double b = angka.nextDouble();
System.out.println();
System.out.print("Masukan nilai c : ");
double c = angka.nextDouble();
System.out.println();
SquareEquationToy s = new SquareEquationToy(a,b,c);
System.out.println("Nilai diskriminan : " +s.calcDiscriminant());
System.out.println();
if (s.calcDiscriminant() > 0){
System.out.println ("Persamaan "+ s.getA() + "X^2" + " + " + s.getB() + "X" + " + "+ s.getC()+ " Memiliki 2 akar berebeda dan keduanya real "+ s.getx1() +" atau "+ s.getx2());
}
else if (s.calcDiscriminant() < 0) {
System.out.println ("Persamaan " + s.getA() + "X^2" + " + " + s.getB() + "X" + " + "+ s.getC()+ " Akar-akar persamaan imaginer ");
}
else {
System.out.println ("Persamaan " + s.getA() + "X^2" + " + " + s.getB() + "X" + " + "+ s.getC()+ " Akar-akar persamaan nyata tetapi kembar, yaitu " + s.imajiner());
}
}
public static void main (String [] args) {
test();
}
}