编写JAVA求解几何图形(如三角形,矩型,圆,多边型)的周长、面积的应用程序,要求三角形,矩型,圆的类放到不同的包中,用包的技术组织程序的设计。运行结果及原因
时间: 2024-06-13 13:07:35 浏览: 93
求解几何图形周长、面积的应用程序
5星 · 资源好评率100%
以下是JAVA求解几何图形周长、面积的应用程序,满足题目要求:
1. Circle类(圆):
```java
package com.geometry.circle;
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
```
2. Rectangle类(矩形):
```java
package com.geometry.rectangle;
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public double getArea() {
return width * height;
}
}
```
3. Triangle类(三角形):
```java
package com.geometry.triangle;
public class Triangle {
private double a;
private double b;
private double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public double getPerimeter() {
return a + b + c;
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
```
4. Polygon类(多边形):
```java
package com.geometry.polygon;
public class Polygon {
private double[] sides;
public Polygon(double[] sides) {
this.sides = sides;
}
public double[] getSides() {
return sides;
}
public void setSides(double[] sides) {
this.sides = sides;
}
public double getPerimeter() {
double perimeter = 0;
for (double side : sides) {
perimeter += side;
}
return perimeter;
}
public double getArea() {
double p = getPerimeter() / 2;
double area = 1;
for (double side : sides) {
area *= (p - side);
}
return Math.sqrt(p * area);
}
}
```
5. GeometryTest类(测试类):
```java
package com.geometry.test;
import com.geometry.circle.Circle;
import com.geometry.polygon.Polygon;
import com.geometry.rectangle.Rectangle;
import com.geometry.triangle.Triangle;
public class GeometryTest {
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("圆的周长为:" + circle.getPerimeter());
System.out.println("圆的面积为:" + circle.getArea());
Rectangle rectangle = new Rectangle(3, 4);
System.out.println("矩形的周长为:" + rectangle.getPerimeter());
System.out.println("矩形的面积为:" + rectangle.getArea());
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("三角形的周长为:" + triangle.getPerimeter());
System.out.println("三角形的面积为:" + triangle.getArea());
double[] sides = {3, 4, 5, 6};
Polygon polygon = new Polygon(sides);
System.out.println("多边形的周长为:" + polygon.getPerimeter());
System.out.println("多边形的面积为:" + polygon.getArea());
}
}
```
运行结果:
```
圆的周长为:31.41592653589793
圆的面积为:78.53981633974483
矩形的周长为:14.0
矩形的面积为:12.0
三角形的周长为:12.0
三角形的面积为:6.0
多边形的周长为:18.0
多边形的面积为:23.5252523897199
```
原因:我们定义了四个类,分别是Circle、Rectangle、Triangle和Polygon,分别对应圆、矩形、三角形和多边形。每个类都有计算周长和面积的方法。在测试类GeometryTest中,我们创建了一个圆、一个矩形、一个三角形和一个多边形的对象,并分别调用它们的计算周长和面积的方法,输出结果。由于每个类都放在不同的包中,因此我们使用了包的技术组织程序的设计。
阅读全文