请定义Triang,circle,rectangle,三个class,按照其特征自行定义好构造函数,并提供求周长和求面积的函数。写出java代码
时间: 2024-05-09 13:16:48 浏览: 17
好的,下面是Java代码:
```
// 定义 Triang 类
class Triang {
private double a, b, c; // 三角形三条边的长度
// 构造函数
public Triang(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
// 求周长的方法
public double getPerimeter() {
return a + b + c;
}
// 求面积的方法(用海龙公式)
public double getArea() {
double p = getPerimeter() / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
// 定义 Circle 类
class Circle {
private double radius; // 圆的半径
// 构造函数
public Circle(double radius) {
this.radius = radius;
}
// 求周长的方法(即圆的周长)
public double getPerimeter() {
return 2 * Math.PI * radius;
}
// 求面积的方法(即圆的面积)
public double getArea() {
return Math.PI * radius * radius;
}
}
// 定义 Rectangle 类
class Rectangle {
private double width, height; // 矩形的宽和高
// 构造函数
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// 求周长的方法(即矩形的周长)
public double getPerimeter() {
return 2 * (width + height);
}
// 求面积的方法(即矩形的面积)
public double getArea() {
return width * height;
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)