package newpacage;
public class Quad {
double a, b, c;
public Quad(double newA, double newB, double newC) {
a = newA;
b = newB;
c = newC;
}
private double x1, x2;
public double getX1() {
return x1;
}
public double getX2() {
return x2;
}
private double b24ac() {
return b * b - 4 * a * c;
}
public void x1x2() throws ANullException, NoRealRootException {
double b24ac;
if (a == 0) {
ANullException a_is_0 = new ANullException("a不可以为0!");
throw a_is_0;
}
b24ac = b24ac();
if (b24ac == 0) {
x1 = (-b) / (2 * a);
System.out.println("x2+2x+1=0根:x1=x2=" + x1);
} else if (b24ac > 0) {
x1 = (-b + Math.sqrt(b24ac()) / (2 * a));
x2 = (-b - Math.sqrt(b24ac()) / (2 * a));
System.out.println("x1=" + x1);
System.out.println("x2=" + x2);
} else {
NoRealRootException rootsException = new NoRealRootException(
"No Real Roots!");
throw rootsException;
}
}
public static void main(String arg[]) throws Exception {
Quad q = new Quad(0, 1, 1);
q.x1x2();
}
class ANullException extends Exception {
ANullException(String msg) {
super(msg);
}
}
class NoRealRootException extends Exception {
NoRealRootException(String msg) {
super(msg);
}
}
}
throws
最新推荐文章于 2022-07-10 13:35:31 发布