throws

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);
	}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值