题目类别: 实验
关键字: 接口的定义、接口实现、多态、对象类型转换
内容要求:
假设已经存在如下计算图形面积之和的类 ShapeUtils.java。
其中:Quadrable 是你需要定义的一个接口,getArea 是该接口中定义的抽象方法。
要求:
定义接口 Quadrable.java
定义多个可计算图形面积的图形类(至少三个:Circle、Rectangle、Triangle)。每个类均需要实现接口 Quadrable,位于 shape 包中。类的其他部分可根据需要设置,如: 定义 Triangle 类的话,可以有表示 3 条边长的数据域。
定义一个 Client 类,其中的 main 方法中建立一个 ArrayList 和一个数组,调用ShapeUtils 类中的两个方法计算面积之和。
实现代码:
Quadrable类:
package action;
/**
* @author zg
*/
public interface Quadrable {
/**
*
* @return 计算图形的面积
*
*/
double getArea();
}
Rectangle类:
package shape;
import action.Quadrable;
/**
* @author zg
*/
public class Rectangle implements Quadrable {
private double length;
private double width;
public Rectangle() {
this(0,0);
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double getArea() {
return width*length;
}
}
Triangle类:
package shape;
import action.Quadrable;
/**
* @author zg
*/
public class Triangle implements Quadrable {
private double a;
private double b;
private double c;
public Triangle() {
this(0,0,0);
}
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;
}
@Override
public double getArea() {
if(a + b <= c || a + c <= b || b + c <= a){
return 0;
}
// 海伦公式
double p=(a+b+c)*0.5;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}
Circle类:
package shape;
import action.Quadrable;
/**
* @author zg
*/
public class Circle implements Quadrable {
private double radius;
public Circle() {
this(0);
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return this.radius*this.radius*Math.PI;
}
}
ShapeUtils类:
package shape;
import action.Quadrable;
import java.util.ArrayList;
/**
* @author zg
*/
public class ShapeUtils {
/**
*
* @param array 数组
* @return 数组中图形的面积之和
*/
public static double totalArea(Quadrable[] array){
double result=0.0;
for (Quadrable shape : array) {
result+=shape.getArea();
}
return result;
}
/**
*
* @param list ArrayList
* @return ArrayList中图形的面积之和
*/
public static double totalArea(ArrayList<Quadrable> list){
double result=0.0;
for (Quadrable shape : list) {
result+=shape.getArea();
}
return result;
}
}
Client类:
package main;
import action.Quadrable;
import shape.Circle;
import shape.Rectangle;
import shape.ShapeUtils;
import shape.Triangle;
import java.util.ArrayList;
/**
* @author zg
*/
public class Client {
public static void main(String[] args) {
ArrayList<Quadrable> shape = new ArrayList<>();
shape.add(new Rectangle(4,6));
shape.add(new Triangle(3,4,5));
shape.add(new Circle(3));
Quadrable[] shape2=new Quadrable[]{
new Rectangle(1,2),
new Triangle(6,8,10),
new Circle(4)
};
System.out.println("ArrayList中图形的面积之和:"+ ShapeUtils.totalArea(shape));
System.out.println("数组中图形的面积之和:"+ ShapeUtils.totalArea(shape2));
}
}