使用开闭原则、MVC结构、GUI,求底为三角形、梯形、园的柱形的体积。
时间: 2023-06-14 18:04:07 浏览: 155
根据开闭原则,我们需要使用抽象类或接口来定义计算体积的方法。同时,使用MVC结构将计算逻辑与界面展示分离开来,方便维护和拓展。
首先,我们可以定义一个Shape接口,包含计算体积的方法:
```java
public interface Shape {
double getVolume(double height);
}
```
然后,我们可以针对不同的底形状(三角形、梯形、园)分别实现一个类来实现Shape接口:
```java
public class Triangle implements Shape {
private double base;
private double height;
public Triangle(double base) {
this.base = base;
}
@Override
public double getVolume(double height) {
return base * height / 2 * height;
}
}
public class Trapezium implements Shape {
private double upperBase;
private double lowerBase;
private double height;
public Trapezium(double upperBase, double lowerBase) {
this.upperBase = upperBase;
this.lowerBase = lowerBase;
}
@Override
public double getVolume(double height) {
return (upperBase + lowerBase) * height / 2 * height;
}
}
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getVolume(double height) {
return Math.PI * radius * radius * height;
}
}
```
最后,我们可以使用GUI来展示界面,并将计算逻辑与界面展示分离开来,实现MVC结构。这里给出一个简单的Swing GUI实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShapeVolumeCalculator extends JFrame {
private JTextField heightField;
private JTextField baseField;
private JTextField upperBaseField;
private JTextField lowerBaseField;
private JTextField radiusField;
private JComboBox<String> shapeBox;
private JLabel resultLabel;
public ShapeVolumeCalculator() {
setTitle("Shape Volume Calculator");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridLayout(7, 2, 10, 10));
JLabel shapeLabel = new JLabel("Shape:");
shapeBox = new JComboBox<>(new String[]{"Triangle", "Trapezium", "Circle"});
mainPanel.add(shapeLabel);
mainPanel.add(shapeBox);
JLabel heightLabel = new JLabel("Height:");
heightField = new JTextField();
mainPanel.add(heightLabel);
mainPanel.add(heightField);
JLabel baseLabel = new JLabel("Base:");
baseField = new JTextField();
mainPanel.add(baseLabel);
mainPanel.add(baseField);
JLabel upperBaseLabel = new JLabel("Upper Base:");
upperBaseField = new JTextField();
mainPanel.add(upperBaseLabel);
mainPanel.add(upperBaseField);
JLabel lowerBaseLabel = new JLabel("Lower Base:");
lowerBaseField = new JTextField();
mainPanel.add(lowerBaseLabel);
mainPanel.add(lowerBaseField);
JLabel radiusLabel = new JLabel("Radius:");
radiusField = new JTextField();
mainPanel.add(radiusLabel);
mainPanel.add(radiusField);
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Shape shape = null;
switch (shapeBox.getSelectedIndex()) {
case 0:
shape = new Triangle(Double.parseDouble(baseField.getText()));
break;
case 1:
shape = new Trapezium(Double.parseDouble(upperBaseField.getText()), Double.parseDouble(lowerBaseField.getText()));
break;
case 2:
shape = new Circle(Double.parseDouble(radiusField.getText()));
break;
}
double volume = shape.getVolume(Double.parseDouble(heightField.getText()));
resultLabel.setText("Volume: " + volume);
}
});
mainPanel.add(new JLabel());
mainPanel.add(calculateButton);
resultLabel = new JLabel();
mainPanel.add(resultLabel);
add(mainPanel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new ShapeVolumeCalculator();
}
}
```
这样,我们就实现了一个根据开闭原则、MVC结构、GUI展示来计算底为三角形、梯形、园的柱形的体积的程序。
阅读全文