java上机 第十三周 任务一 一元二次方程求解
/* * 程序头部注释开始 * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:一元二次方程求解 * 作 者:薛广晨 * 完成日期:2012 年 11 月 21日 * 版 本号:x1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述:封装一个求一元二次方程根的类SquareEquation,要求考虑解方程的异常,再编写一个窗口类EquationFrame。 要求窗口使用三个文本框和一个文本区,其中三个文本框用来显示生更新方程对象的系数,文本区用来显示方程的根。 窗口中有一个按钮,用户单击按钮后,程序用文本框中的数据修改方程的系数,并将方程的根显示在文本区中* 程序输出: * 程序头部的注释结束 *///SquareEquation类package xue;public class SquareEquation {private double a;private double b;private double c;private double root1;private double root2;SquareEquation(){this.a = 0;this.b = 0;this.c = 0;}SquareEquation(double a, double b, double c){this.a = a;this.b = b;this.c = c;}public double getA() {return a;}public void setA(double a) {this.a = a;}public double getB() {return b;}public void setB(double b) {this.b = b;}public double getC() {return c;}public void setC(double c) {this.c = c;}public double getRootOne() throws NoRealRootException,NoSquareEquationException{ if(a!=0) { double d = b*b - 4*a*c; if(d >= 0) { root1=(-b+Math.sqrt(d))/(2*a); } else { throw new NoRealRootException("没有实根"); } } else { throw new NoRealRootException("不是一元二次方程"); } return root1;} public double getRootTwo() throws NoRealRootException,NoSquareEquationException{ if(a!=0) { double d=b*b-4*a*c; if(d>=0) { root2=(-b-Math.sqrt(d))/(2*a); } else { throw new NoRealRootException("没有实根"); } } else { throw new NoRealRootException("不是一元二次方程"); } return root2;} }class NoRealRootException extends Exception{ String message; NoRealRootException(String s){ message=s;} public String getMessage() { return message; }}class NoSquareEquationException extends Exception{ String message; NoSquareEquationException(String s) { message=s; } public String getMessage() { return message; }}//EquationFrame类package xue;import java.awt.*;import java.awt.event.*;public class EquationFrame extends Frame implements ActionListener{TextField tf1;TextField tf2;TextField tf3; Label label1; Label label2; Label label3; TextArea showRoots; Button button; SquareEquation se; EquationFrame() { setTitle("求一元二次方程的根"); tf1 = new TextField(8); tf2 = new TextField(8); tf3 = new TextField(8); label1 = new Label("二次项系数:"); label2 = new Label("一次项系数:"); label3 = new Label("常数项系数:"); showRoots = new TextArea(5, 40); button = new Button("确定"); se = new SquareEquation(); setLayout(new FlowLayout()); setBounds(100, 100, 640, 200); add(label1); add(tf1); add(label2); add(tf2); add(label3); add(tf3); add(button); add(showRoots); button.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); validate(); }public void actionPerformed(ActionEvent e) {showRoots.setText(null);try{double a = Double.parseDouble(tf1.getText());double b = Double.parseDouble(tf2.getText());double c = Double.parseDouble(tf3.getText());se.setA(a);se.setB(b);se.setC(c);showRoots.append("\n 根:" + se.getRootOne());showRoots.append("\n 根:" + se.getRootTwo());}catch(Exception ex){showRoots.append("\n"+ex+"\n");}}}//测试类 EquationFrameTestpackage xue;public class EquationFrameTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubnew EquationFrame();}}
运行结果: