第一题:定义接口Shape和实现该接口的Circle类和Rectangle类
提供接口Shape1
和两个类Circle5
和Rectangle5
的实现。
在一个包内定义一个接口Shape,内含一个变量PI,一个方法:area()。定义Circle类和Rectangle类,实现接口Shape,重写area()方法
package shiyan7;
public interface Shape1 {
// 内含一个变量PI,一个方法:area()。
double PI= 3.14;
double area();
}
//Circle5圆
class Circle5 implements Shape1{
double sum;
double r;
Circle5(double r){
this.r=r;
}
@Override
public double area() {
sum = PI*this.r*this.r;
return sum;
}
}
//Rectangle5 长方形
class Rectangle5 implements Shape1{
double Length;
double Width;
Rectangle5(double Length,double Width){
this.Length = Length;
this.Width = Width;
}
@Override
public double area() {
return this.Length*this.Width;
}
}
package shiyan7;
public class test {
public static void main(String args[]){
Circle5 S1= new Circle5(2);
Rectangle5 S2= new Rectangle5(8,6);
System.out.println("圆的面积:"+S1.area());
System.out.println("长方形的面积:"+S2.area());
}
}
在另一个包中定义测试类,创建Shape类型的数组
提供了Shape
接口和几个实现该接口的类,以及一个Calculate
类来计算面积。接下来,需要创建一个测试类TestShapes
,它将使用Shape
类型的数组来存储不同的图形对象,并计算它们的面积。
在另一个包中定义测试类,创建Shape类型的数组,调用数组元素的area()方法,输出相应信息。(也可以定义Calculate类,包含cal(Shapes)方法来计算s的面积。在main()方法中创建多个对象作为参数调用cal()方法,输出相应面积信息。)
package shiyan7_1;
public interface Shape {
double PI = 3.14159;
double area();
}
public class Circle5_1 implements Shape {
private double radius;
public Circle5_1 (double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * radius * radius;
}
}
public class Rectangle5_1 implements Shape {
private double length;
private double width;
public Rectangle5_1(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double area() {
return length * width;
}
}
public class ladder5_1 implements Shape {
private double height;
private double base1, base2;
public ladder5_1(double base1, double base2, double height) {
this.base1 = base1;
this.base2 = base2;
this.height = height;
}
@Override
public double area() {
return (base1 + base2) * height / 2;
}
}
package shiyan7_2;
import shiyan7_1.Shape;
public class Calculate {
public static double cal(Shape s) {
return s.area();
}
}
package shiyan7_2;
import shiyan7_1.Shape;
import shiyan7_1.Rectangle5_1;
import shiyan7_1.Circle5_1;
import shiyan7_1.ladder5_1;
public class TestShapes {
public static void main(String[] args) {
Shape[] shapes = new Shape[]
{new Circle5_1 (5), new Rectangle5_1(8, 6)};
for (Shape shape : shapes) {
System.out.println("面积: " + shape.area());
}}
添加新的图形梯形,并将其对象应用到第二题中
已经定义了ladder5_1
类来表示梯形,并实现了Shape
接口。需要在TestShapes
类中添加一个梯形对象,并使用Calculate
类的cal
方法来计算其面积。
package shiyan7_2;
import shiyan7_1.Shape;
import shiyan7_1.Rectangle5_1;
import shiyan7_1.Circle5_1;
import shiyan7_1.ladder5_1;
public class TestShapes {
public static void main(String[] args) {
Shape circle = new Circle5_1(5);
Shape rectangle = new Rectangle5_1(8, 6);
Shape ladder = new ladder5_1(6,10,8);
System.out.println("圆的面积是: " + Calculate.cal(circle));
System.out.println("长方体的面积是: " + Calculate.cal(rectangle));
System.out.println("梯形的面积是: " + Calculate.cal(ladder));
}