import javax.swing.*; class Quadratic { public static void main(String[] args) { String aStr,bStr,cStr; double a,b,c; double disc,r1,r2; aStr = JOptionPane.showInputDialog(null,"Enter coefficient of x^2 "); bStr = JOptionPane.showInputDialog(null,"Enter coefficient of x "); cStr = JOptionPane.showInputDialog(null,"Enter constant coefficient "); a = Double.parseDouble(aStr); b = Double.parseDouble(bStr); c = Double.parseDouble(cStr); if (a == 0) { if (b==0) { if (c==0) JOptionPane.showMessageDialog(null,"All x are solutions"); else JOptionPane.showMessageDialog(null,"No x are solutions"); } else JOptionPane.showMessageDialog(null,"the only solution is "+ -c/b); } else { disc = b*b-4*a*c; if (disc>0) { r1 = (-b+Math.sqrt(disc))/(2*a); r2 = (-b-Math.sqrt(disc))/(2*a); JOptionPane.showMessageDialog(null,"The two real solutions are " + r1 + " and " + r2); } else if (disc == 0) JOptionPane.showMessageDialog(null,"The only solution is " + -b/(2*a)); else { double realpart, imagpart; realpart = -b/(2*a); imagpart = Math.sqrt(-disc)/(2*a); if (realpart != 0) JOptionPane.showMessageDialog(null,"The 2 complex solutions are \n"+ realpart + " + " + imagpart + " I " + "\n" + realpart + " - " + imagpart + " I "); else JOptionPane.showMessageDialog(null,"The 2 complex solutions are \n"+ imagpart + " I " + "\n" + " - " + imagpart + " I "); } } System.exit(0); } }