窗体程序计算一元二次方程

问题描述:

 设计一个一元二次方程类,包含三个属性(方程的3个系数),包含计算器求根的方法。

窗体中使用三个文本框和一个文本区,其中三个文本框用来输入方程的系数;文本区用来显示方程的根。

窗体中有一个按钮,用户点击该按钮后,程序获取三个系数构建仿佛方程对象,并调用方法计算方程的根在文本区中显示。

1.定义一元二次方程类SquareEquation

public class SquareEquation {
double a,b,c;
public void setA(double a){
	this.a=a;
}
public void setB(double b){
	this.b=b;
}
public void setC(double c){
	this.c=c;
}
public double getRootOne()
{
	double disk=calculateValidDisk();
	return (-b+Math.sqrt(disk))/(2*a);
}
public double getRootTwo(){
	double disk=calculateValidDisk();
	return (-b-Math.sqrt(disk))/(2*a);
}
private double calculateValidDisk()
{
	if(a==0){
		throw new EquationException("不是二次方程",
				EquationException.NONE_EQUATION);
	}
	double disk=b*b-4*a*c;
	if(disk<0)
	{throw new EquationException("没有实根",
			EquationException.NO_REAL_ROOT);
	}
	return disk;
}
}
class EquationException extends RuntimeException{
	public static final int NONE_EQUATION=1;
	public static final int NO_REAL_ROOT=2;
	private int errorCode;
public EquationException(String msg,int errerCode)
{
	super(msg);
	this.errorCode=errorCode;
}
public int getErroeCode(){
	return errorCode;
}
}


2.定义窗体类EquationFrame,继承自JFrame

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EquationFrame extends JFrame implements ActionListener{
	SquareEquation equation;
	JTextField textA,textB,textC;
	JTextArea showRoots;
	JButton controlButton;
	public EquationFrame(){
		equation=new SquareEquation();
		textA=new JTextField(8);
		textB=new JTextField(8);
		textC=new JTextField(8);
		controlButton =new JButton("确定");
		JPanel pNorth=new JPanel();
		pNorth.add(new JLabel("二次项系数:"));
		pNorth.add(textA);
		pNorth.add(new JLabel("一次项系数:"));
		pNorth.add(textB);
		pNorth.add(new JLabel("常数项系数:"));
		pNorth.add(textC);
pNorth.add(controlButton);
controlButton.addActionListener(this);
getContentPane().add(pNorth,BorderLayout.NORTH);
showRoots=new JTextArea();
JScrollPane scrollPane=new JScrollPane(showRoots);
getContentPane().add(scrollPane,BorderLayout.CENTER);
setSize(630,160);
Dimension scnSize=Toolkit.getDefaultToolkit().getDefaultToolkit().getScreenSize();
Dimension frmSize=this.getSize();
this.setLocation((scnSize.width-frmSize.width)/2,
		   (scnSize.height-frmSize.height)/2);
validate();
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e){
		try{
			double a=Double.parseDouble(textA.getText());
			double b=Double.parseDouble(textB.getText());
			double c=Double.parseDouble(textC.getText());
			equation.setA(a);
			equation.setB(b);
			equation.setC(c);
			showRoots.append("根: "+equation.getRootOne());
			showRoots.append(" 根:"+equation.getRootTwo()+"\n");
		}catch(Exception ex)
		{
			showRoots.append(ex.getMessage()+"\n");
		}
	}
}


3.测试类

public class Text {
	public static void main(String args[])
	{
		new EquationFrame();
	}
}


4.运行结果


  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎曼猜想·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值