任务描述 工厂切割金属板材后,会产出很多圆形或者矩形的废弃材料。工厂新采购了一套激光测距工具,可以远距离测量圆形的半径和矩形的长和宽。 现在已经完成测量工作,需要用程序计算出这批废弃材料中面积最大和最小的形状。JAVA代码
时间: 2024-02-26 17:58:52 浏览: 15
以下是Java示例代码,实现计算废弃材料中面积最大和最小的形状。
```java
import java.util.ArrayList;
import java.util.List;
import java.lang.Math;
class Shape {
private String shapeType;
private double length;
private double width;
private double radius;
public Shape(String shapeType, double length, double width, double radius) {
this.shapeType = shapeType;
this.length = length;
this.width = width;
this.radius = radius;
}
public String getShapeType() {
return shapeType;
}
public double getArea() {
if (shapeType.equals("circle")) {
return Math.PI * radius * radius;
} else {
return length * width;
}
}
}
public class Main {
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<Shape>();
// 读取测量结果,并创建 Shape 对象
try {
Scanner scanner = new Scanner(new File("measurements.txt"));
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(",");
String shapeType = parts[0];
double length = Double.parseDouble(parts[1]);
double width = 0;
double radius = 0;
if (shapeType.equals("circle")) {
radius = Double.parseDouble(parts[2]);
} else {
width = Double.parseDouble(parts[2]);
}
Shape shape = new Shape(shapeType, length, width, radius);
shapes.add(shape);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 找到面积最大和最小的形状
double maxArea = Double.MIN_VALUE;
double minArea = Double.MAX_VALUE;
Shape maxShape = null;
Shape minShape = null;
for (Shape shape : shapes) {
double area = shape.getArea();
if (area > maxArea) {
maxArea = area;
maxShape = shape;
}
if (area < minArea) {
minArea = area;
minShape = shape;
}
}
// 输出结果
System.out.println("最大面积形状为:" + maxShape.getShapeType() + ",面积为:" + maxArea);
System.out.println("最小面积形状为:" + minShape.getShapeType() + ",面积为:" + minArea);
}
}
```
请注意,此示例假设测量结果保存在一个名为 `measurements.txt` 的文件中,每行包含一个形状的类型和测量结果。实际应用中,可能需要根据实际情况进行修改。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)