Java实验14 计算多个二维图形的面积之和

题目类别: 实验


关键字: 接口的定义、接口实现、多态、对象类型转换


内容要求:

假设已经存在如下计算图形面积之和的类 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));
    }
}



在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值