You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.3 KiB
68 lines
2.3 KiB
package com.example;
|
|
|
|
/**
|
|
* 图形面积计算器第一版(不使用多态)
|
|
*/
|
|
public class ShapeCalculator {
|
|
|
|
// 计算圆形面积
|
|
public double calculateCircleArea(double radius) {
|
|
return Math.PI * radius * radius;
|
|
}
|
|
|
|
// 计算矩形面积
|
|
public double calculateRectangleArea(double width, double height) {
|
|
return width * height;
|
|
}
|
|
|
|
// 计算三角形面积
|
|
public double calculateTriangleArea(double base, double height) {
|
|
return base * height / 2;
|
|
}
|
|
|
|
// 绘制圆形
|
|
public void drawCircle(double radius) {
|
|
System.out.println("绘制一个半径为 " + radius + " 的圆形");
|
|
}
|
|
|
|
// 绘制矩形
|
|
public void drawRectangle(double width, double height) {
|
|
System.out.println("绘制一个宽为 " + width + ",高为 " + height + " 的矩形");
|
|
}
|
|
|
|
// 绘制三角形
|
|
public void drawTriangle(double base, double height) {
|
|
System.out.println("绘制一个底为 " + base + ",高为 " + height + " 的三角形");
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
ShapeCalculator calculator = new ShapeCalculator();
|
|
|
|
// 测试圆形
|
|
double circleRadius = 5.0;
|
|
double circleArea = calculator.calculateCircleArea(circleRadius);
|
|
System.out.println("圆形的半径: " + circleRadius);
|
|
System.out.println("圆形的面积: " + circleArea);
|
|
calculator.drawCircle(circleRadius);
|
|
|
|
System.out.println();
|
|
|
|
// 测试矩形
|
|
double rectWidth = 4.0;
|
|
double rectHeight = 6.0;
|
|
double rectArea = calculator.calculateRectangleArea(rectWidth, rectHeight);
|
|
System.out.println("矩形的宽: " + rectWidth + ",高: " + rectHeight);
|
|
System.out.println("矩形的面积: " + rectArea);
|
|
calculator.drawRectangle(rectWidth, rectHeight);
|
|
|
|
System.out.println();
|
|
|
|
// 测试三角形
|
|
double triangleBase = 8.0;
|
|
double triangleHeight = 3.0;
|
|
double triangleArea = calculator.calculateTriangleArea(triangleBase, triangleHeight);
|
|
System.out.println("三角形的底: " + triangleBase + ",高: " + triangleHeight);
|
|
System.out.println("三角形的面积: " + triangleArea);
|
|
calculator.drawTriangle(triangleBase, triangleHeight);
|
|
}
|
|
}
|