使用java代码实现使用开闭原则、MVC结构、GUI,求底为三角形、梯形、园的柱形的体积。
时间: 2023-10-22 17:03:35 浏览: 100
java_java练习题_
首先,我需要明确一下您的需求,您需要实现一个GUI程序,用户可以输入三角形、梯形或圆柱的底面信息和高度,程序可以计算出该图形的体积并显示在界面上,是吗?
如果是这样的话,我可以给您提供一些思路和代码示例。
1. 开闭原则
开闭原则要求系统中的代码可以扩展而不需要修改。为了实现这个原则,我们可以将具体的计算逻辑抽象成接口或抽象类,并通过多态来实现具体的计算。这样在新增一个图形时,只需要实现这个接口或继承这个抽象类,而不需要修改原有的代码。
2. MVC结构
MVC结构将程序分为三个部分:模型、视图和控制器。模型负责存储数据和计算逻辑,视图负责界面展示,控制器负责处理用户输入和调用模型计算,将结果传递给视图展示。这种结构可以使程序更加清晰、易于维护和扩展。
3. GUI界面
GUI界面可以使用Swing或JavaFX等库来实现。需要注意的是,界面应该与计算逻辑分离,不应该在界面代码中直接进行计算。
下面是一份简单的代码示例:
模型类:
```java
public interface Shape {
double getArea(); // 计算底面积
double getHeight(); // 获取高度
double getVolume(); // 计算体积
}
public class Triangle implements Shape {
private double base; // 底边长
private double height; // 高度
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
@Override
public double getHeight() {
return height;
}
@Override
public double getVolume() {
return getArea() * getHeight();
}
}
public class Trapezoid implements Shape {
private double top; // 上底长度
private double bottom; // 下底长度
private double height; // 高度
public Trapezoid(double top, double bottom, double height) {
this.top = top;
this.bottom = bottom;
this.height = height;
}
@Override
public double getArea() {
return (top + bottom) * height / 2;
}
@Override
public double getHeight() {
return height;
}
@Override
public double getVolume() {
return getArea() * getHeight();
}
}
public class Cylinder implements Shape {
private double radius; // 半径
private double height; // 高度
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getHeight() {
return height;
}
@Override
public double getVolume() {
return getArea() * getHeight();
}
}
```
控制器类:
```java
public class CalculatorController {
private CalculatorView view;
public CalculatorController(CalculatorView view) {
this.view = view;
}
public void calculate() {
try {
double base = Double.parseDouble(view.getBase());
double top = Double.parseDouble(view.getTop());
double radius = Double.parseDouble(view.getRadius());
double height = Double.parseDouble(view.getHeight());
Shape shape;
if (view.isTriangle()) {
shape = new Triangle(base, height);
} else if (view.isTrapezoid()) {
shape = new Trapezoid(top, base, height);
} else {
shape = new Cylinder(radius, height);
}
double volume = shape.getVolume();
view.setVolume(volume);
} catch (NumberFormatException e) {
view.showError("请输入正确的数值");
}
}
}
```
视图类:
```java
public class CalculatorView extends JFrame {
private JRadioButton triangleRadioButton;
private JRadioButton trapezoidRadioButton;
private JRadioButton cylinderRadioButton;
private JTextField baseTextField;
private JTextField topTextField;
private JTextField radiusTextField;
private JTextField heightTextField;
private JButton calculateButton;
private JLabel volumeLabel;
public CalculatorView() {
initUI();
initController();
}
private void initUI() {
setLayout(new GridLayout(5, 2));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
triangleRadioButton = new JRadioButton("三角形");
trapezoidRadioButton = new JRadioButton("梯形");
cylinderRadioButton = new JRadioButton("圆柱");
ButtonGroup group = new ButtonGroup();
group.add(triangleRadioButton);
group.add(trapezoidRadioButton);
group.add(cylinderRadioButton);
add(triangleRadioButton);
add(new JLabel());
add(trapezoidRadioButton);
add(new JLabel());
add(cylinderRadioButton);
add(new JLabel());
add(new JLabel("底边长"));
baseTextField = new JTextField();
add(baseTextField);
add(new JLabel("上底长"));
topTextField = new JTextField();
add(topTextField);
add(new JLabel("半径"));
radiusTextField = new JTextField();
add(radiusTextField);
add(new JLabel("高度"));
heightTextField = new JTextField();
add(heightTextField);
calculateButton = new JButton("计算体积");
add(calculateButton);
add(new JLabel("体积"));
volumeLabel = new JLabel();
add(volumeLabel);
pack();
setVisible(true);
}
private void initController() {
CalculatorController controller = new CalculatorController(this);
calculateButton.addActionListener(e -> controller.calculate());
}
public boolean isTriangle() {
return triangleRadioButton.isSelected();
}
public boolean isTrapezoid() {
return trapezoidRadioButton.isSelected();
}
public String getBase() {
return baseTextField.getText();
}
public String getTop() {
return topTextField.getText();
}
public String getRadius() {
return radiusTextField.getText();
}
public String getHeight() {
return heightTextField.getText();
}
public void setVolume(double volume) {
volumeLabel.setText(String.format("%.2f", volume));
}
public void showError(String message) {
JOptionPane.showMessageDialog(this, message, "错误", JOptionPane.ERROR_MESSAGE);
}
}
```
启动类:
```java
public class Main {
public static void main(String[] args) {
new CalculatorView();
}
}
```
这份代码实现了您的需求,用户可以选择三角形、梯形或圆柱,输入底面信息和高度,点击计算按钮,程序会计算出该图形的体积并展示在界面上。这份代码还实现了开闭原则和MVC结构,使得程序易于扩展和维护。
阅读全文