Java用Mvc写一个一元二次方程
时间: 2024-01-02 10:03:05 浏览: 68
好的,以下是使用Java MVC模式设计的一元二次方程计算器:
Model(模型):Equation.java
```java
public class Equation {
private double a, b, c;
private double discriminant;
private double root1, root2;
public Equation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public void solve() {
discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
root1 = Double.NaN;
root2 = Double.NaN;
} else if (discriminant == 0) {
root1 = -b / (2 * a);
root2 = root1;
} else {
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
}
}
public double getRoot1() {
return root1;
}
public double getRoot2() {
return root2;
}
}
```
View(视图):EquationView.java
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class EquationView extends JFrame {
private JTextField aField = new JTextField(5);
private JTextField bField = new JTextField(5);
private JTextField cField = new JTextField(5);
private JTextField root1Field = new JTextField(10);
private JTextField root2Field = new JTextField(10);
private JButton solveButton = new JButton("Solve");
public EquationView() {
JPanel equationPanel = new JPanel(new GridLayout(2, 3));
equationPanel.add(new JLabel("a:"));
equationPanel.add(aField);
equationPanel.add(new JLabel("x^2 +"));
equationPanel.add(new JLabel("b:"));
equationPanel.add(bField);
equationPanel.add(new JLabel("x +"));
equationPanel.add(new JLabel("c:"));
equationPanel.add(cField);
equationPanel.add(new JLabel("="));
JPanel rootPanel = new JPanel(new GridLayout(2, 2));
rootPanel.add(new JLabel("Root 1:"));
rootPanel.add(root1Field);
rootPanel.add(new JLabel("Root 2:"));
rootPanel.add(root2Field);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(solveButton);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(equationPanel, BorderLayout.CENTER);
mainPanel.add(rootPanel, BorderLayout.SOUTH);
mainPanel.add(buttonPanel, BorderLayout.EAST);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Quadratic Equation Solver");
this.setSize(400, 150);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.add(mainPanel);
}
public double getA() {
return Double.parseDouble(aField.getText());
}
public double getB() {
return Double.parseDouble(bField.getText());
}
public double getC() {
return Double.parseDouble(cField.getText());
}
public void setRoot1(double root1) {
root1Field.setText(Double.toString(root1));
}
public void setRoot2(double root2) {
root2Field.setText(Double.toString(root2));
}
public void addSolveListener(ActionListener listener) {
solveButton.addActionListener(listener);
}
}
```
Controller(控制器):EquationController.java
```java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EquationController {
private EquationView view;
private Equation model;
public EquationController(EquationView view, Equation model) {
this.view = view;
this.model = model;
view.addSolveListener(new SolveListener());
}
class SolveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
double a = view.getA();
double b = view.getB();
double c = view.getC();
model = new Equation(a, b, c);
model.solve();
view.setRoot1(model.getRoot1());
view.setRoot2(model.getRoot2());
}
}
}
```
Main(主函数):Main.java
```java
public class Main {
public static void main(String[] args) {
Equation model = new Equation(1, 1, -6);
EquationView view = new EquationView();
EquationController controller = new EquationController(view, model);
view.setVisible(true);
}
}
```
在运行程序时,将显示一个窗口,用户可以输入一元二次方程的系数,并点击“Solve”按钮计算方程的根。计算结果将显示在窗口下方的文本框中。
注意:为了使代码更简洁,此处省略了一些异常处理和输入验证等细节。在实际开发中,应该对用户输入进行验证,以确保程序的正确性和健壮性。
阅读全文