Java编程实现图形计算,可以通过计算不同图形的面积输出结果 图形包括平行四边形、矩形、三角形、圆形等 实现思路: 1.定义形状类Shape,包含方法CalculatedArea() 2.定义形状类的子类:矩形矩形、平行四边形Parallelofour、圆形圆圈和三角形三角形 3.定义运算类Operation,可以计算所有形状的面积,最后用测试类输出结果怎么写
时间: 2023-06-10 08:03:58 浏览: 42
感谢您的提问!以下是我的回答:
您可以按照以下步骤来实现Java编程实现图形计算:
1.定义形状类Shape,包含方法CalculatedArea()
```
public abstract class Shape {
public abstract double calculatedArea();
}
```
2.定义形状类的子类:矩形矩形、平行四边形Parallelofour、圆形圆圈和三角形三角形
```
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculatedArea() {
return length * width;
}
}
public class Parallelogram extends Shape {
private double base;
private double height;
public Parallelogram(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double calculatedArea() {
return base * height;
}
}
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculatedArea() {
return Math.PI * Math.pow(radius, 2);
}
}
public class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double calculatedArea() {
return 0.5 * base * height;
}
}
```
3.定义运算类Operation,可以计算所有形状的面积
```
public class Operation {
public static double calculatedArea(Shape shape) {
return shape.calculatedArea();
}
}
```
4.用测试类输出结果
```
public class Test {
public static void main(String[] args) {
Shape rectangle = new Rectangle(5, 6);
Shape parallelogram = new Parallelogram(4, 7);
Shape circle = new Circle(2.5);
Shape triangle = new Triangle(3, 4);
System.out.println("矩形的面积:" + Operation.calculatedArea(rectangle));
System.out.println("平行四边形的面积:" + Operation.calculatedArea(parallelogram));
System.out.println("圆形的面积:" + Operation.calculatedArea(circle));
System.out.println("三角形的面积:" + Operation.calculatedArea(triangle));
}
}
```
这样,您就可以成功实现Java编程实现图形计算了!
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)