Java--13週目の実験--一元二次方程式のルートを求めるクラスSquareEquationをカプセル化し、方程式を解く異常を考慮し、ウィンドウクラスEquationFrameを作成することを要求した.
3062 ワード
Testクラス:
EquationFrameクラス:
SquareEquationクラス:
実験結果:
/* ( )
*
* Copyright (c) 2011,
* All rights reserved.
* : SquareEquation, , EquationFrame。
* :
* : 2012 11 20
* : V1.0
*
* : SquareEquation, , EquationFrame。
* : , , 。
* : , , , 。
* : 。
* :
* :
*
*/
package task_one;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new EquationFrame();
}
}
EquationFrameクラス:
package task_one;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
// , , 。
// , , , 。
public class EquationFrame extends JFrame implements ActionListener{
JTextField text1,text2,text3;
JTextArea text4;
JButton button;
JLabel lable1,lable2,lable3;
public EquationFrame(){
lable1 = new JLabel(" :");
lable2 = new JLabel(" :");
lable3 = new JLabel(" :");
button = new JButton(" ");
text1 = new JTextField(6);
text2 = new JTextField(6);
text3 = new JTextField(6);
text4 = new JTextArea(" ", 6, 15);
add(lable1);
add(text1);
add(lable2);
add(text2);
add(lable3);
add(text3);
add(button);
add(text4);
button.addActionListener(this);
setLayout(new FlowLayout());
setBounds(500,500,900,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
SquareEquation squareEquation = new SquareEquation();
squareEquation.setA(Integer.parseInt(text2.getText()));
squareEquation.setB(Integer.parseInt(text1.getText()));
squareEquation.setC(Integer.parseInt(text3.getText()));
text4.append(" :"+squareEquation.count_one());
text4.append(" :"+squareEquation.count_two());
}
}
SquareEquationクラス:
package task_one;
import java.lang.Math;
// SquareEquation, ,
public class SquareEquation {
private int a,b,c;
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
public void setC(int c) {
this.c = c;
}
public String count_one(){
double m = b *b -4 * a * c;
double d = 0;
try{
d = Math.sqrt(m);
}
catch(Exception e)
{
System.out.println(" :"+e.getMessage());
}
double i = (-b-d)/(2*a);
String s = String.valueOf(i);
return s;
}
public String count_two(){
double m = b *b -4 * a * c;
double d = 0;
try{
d = Math.sqrt(m);
}
catch(Exception e)
{
System.out.println(" :"+e.getMessage());
}
double i = (-b+d)/(2*a);
String s = String.valueOf(i);
return s;
}
}
実験結果: