写一个完整的Java Application程序,包含接口ShapeArea,以及MyRectangle类,MyTriabgle类和Test类
时间: 2023-09-13 16:05:26 浏览: 153
### 回答1:
public interface ShapeArea {
double getArea();
}class MyRectangle implements ShapeArea {
private double length;
private double width;
MyRectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}
class MyTriangle implements ShapeArea {
private double base;
private double height;
MyTriangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getArea() {
return (base * height) / 2;
}
}public class Test {
public static void main(String[] args) {
MyRectangle rect = new MyRectangle(3, 4);
MyTriangle tri = new MyTriangle(3, 4);
System.out.println("Area of Rectangle: " + rect.getArea());
System.out.println("Area of Triangle: " + tri.getArea());
}
}
答:以上是一个完整的Java应用程序,它包含一个接口ShapeArea,以及MyRectangle类,MyTriabgle类和Test类。
### 回答2:
接口ShapeArea定义了一个计算形状面积的方法getArea()。MyRectangle类和MyTriangle类分别实现了ShapeArea接口,并实现了getArea()方法来计算矩形和三角形的面积。Test类用于测试这些类的功能。
ShapeArea接口的代码如下:
```java
interface ShapeArea {
double getArea();
}
```
MyRectangle类的代码如下:
```java
class MyRectangle implements ShapeArea {
private double width;
private double height;
public MyRectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
}
```
MyTriangle类的代码如下:
```java
class MyTriangle implements ShapeArea {
private double base;
private double height;
public MyTriangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getArea() {
return 0.5 * base * height;
}
}
```
Test类的代码如下:
```java
public class Test {
public static void main(String[] args) {
MyRectangle rectangle = new MyRectangle(5, 10);
MyTriangle triangle = new MyTriangle(4, 8);
double rectangleArea = rectangle.getArea();
double triangleArea = triangle.getArea();
System.out.println("矩形的面积: " + rectangleArea);
System.out.println("三角形的面积: " + triangleArea);
}
}
```
在Test类中,我们创建了一个MyRectangle对象和一个MyTriangle对象,并分别调用它们的getArea()方法来计算它们的面积。最后,我们通过println方法将计算得到的面积输出到控制台。
以上就是一个完整的Java Application程序,包含接口ShapeArea和实现该接口的MyRectangle类、MyTriangle类以及用于测试的Test类。
### 回答3:
接口ShapeArea定义如下:
```java
public interface ShapeArea {
double getArea();
}
```
MyRectangle类实现ShapeArea接口,并定义矩形的属性和方法:
```java
public class MyRectangle implements ShapeArea {
private double width;
private double height;
public MyRectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
}
```
MyTriangle类实现ShapeArea接口,并定义三角形的属性和方法:
```java
public class MyTriangle implements ShapeArea {
private double base;
private double height;
public MyTriangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getArea() {
return 0.5 * base * height;
}
}
```
Test类用于测试MyRectangle和MyTriangle类的功能:
```java
public class Test {
public static void main(String[] args) {
MyRectangle rectangle = new MyRectangle(5, 10);
double rectangleArea = rectangle.getArea();
System.out.println("矩形的面积为:" + rectangleArea);
MyTriangle triangle = new MyTriangle(5, 10);
double triangleArea = triangle.getArea();
System.out.println("三角形的面积为:" + triangleArea);
}
}
```
以上就是一个完整的Java Application程序,使用接口ShapeArea,以及MyRectangle类,MyTriangle类和Test类实现了对矩形和三角形面积的计算和输出。
阅读全文