Baiklah kali ini saya akan membuat program Java menggunakan
SquareEquationToy. Berikut adalah listing programnya:
import javax.swing.*;
public class SquareEquationToy {
private double a;
private double b;
private double c;
private double x1;
private double x2;
private boolean fValid = false;
public
SquareEquationToy()
{
{
String input = JOptionPane.showInputDialog("masukan nilai a");
double
a = Double.parseDouble(input);
System.out.println("nilai
a = " + a);
String
Input = JOptionPane.showInputDialog("masukan nilai b");
double
b = Double.parseDouble(Input);
System.out.println("nilai
b = " + b);
String
iNput = JOptionPane.showInputDialog("masukan nilai c");
double
c = Double.parseDouble(iNput);
System.out.println("nilai
c = " + c);
this.a
= a;
this.b
= b;
this.c
= c;
calcX();
}
private
double calcDiscriminant(){
return
b*b - 4*a*c;
}
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(){
SquareEquationToy
s = new SquareEquationToy ();
if
(s.isValid()){
System.out.println("Persamaan
"+s.getA()+"X^2 "+s.getB()+"X"+s.getC() + "
mempunyai akar "+s.getX1()+"dan"+s.getX2());
}
else
{
System.out.println("Persamaan
"+s.getA()+"X^2 "+s.getB()+"X"+s.getC() + "
Tidak mempunyai akar-akar riil");
}
}
public
static void main (String[]args){
test();
}
System.exit(0);
}}
Mudah bukan program tersebut?jika program tidak berjalan
dengan baik,coba anda koreksi besar atau kecilnya huruf yang anda gunakan,karena
program ini sangat sensitif.
SELAMAT MENCOBA ^.^