Just try to understand each steps.
Code:
import javax.swing.JOptionPane;
class Main {
public static void main(String[] args) {
double P, Q, R, r1, r2, disc;
String IS;
System.out.println(" computes reads the quadratic formula." +
"\nStandard Form of quadratic equations: Px^2 + Qx + R = 0" +
"\n dtermine whether roots are imaginary!\n");
IS = JOptionPane.showInputDialog("Px^2 + Qx + R = 0\n enter P:");
P = Double.parseDouble(IS);
IS = JOptionPane.showInputDialog("Px^2 + Qx + C = 0\n enter Q:");
Q = Double.parseDouble(IS);
IS = JOptionPane.showInputDialog("Px^2 +Qx + R = 0\n enter R:");
R = Double.parseDouble(IS);
disc = (Math.pow(Q, 2)) - (4*P*R);
if(disc == 0.0) {
root1 = (-Q + Math.sqrt(disc)) / (2 *P);
JOptionPane.showMessageDialog(null, String.format("There is one root at: %.4f\n\n", root1), "Roots:", JOptionPane.INFORMATION_MESSAGE);
}
else {
if(disc > 0.0) {
root1 = (-Q + Math.sqrt(disc)) / (2 * P);
root2 = (-Q - Math.sqrt(disc)) / (2 * P);
JOptionPane.showMessageDialog(null, String.format("There are two real roots at: %.4f and %.4f\n\n", r1, r2), "Roots:", JOptionPane.INFORMATION_MESSAGE);
}
else {
if(disc < 0.0) {
root1 = (-Q + Math.sqrt(-disc)) / (2 * P);
root2 = (-Q + Math.sqrt(-disc)) / (2 * P);
JOptionPane.showMessageDialog(null, String.format("There are two imaginary roots at: %.4fi and %.4fi\n\n", r1, r2), "Roots:", JOptionPane.INFORMATION_MESSAGE);
}
}
}
System.exit(0);
}
}
Bookmarks