哪个大佬救救孩子吧,想不出来怎么写

qq_45351181 2020-07-03 08:07:54
(1)设计一个形状类 Shape,包含一个 getArea()方法,该方法不包含实际语句。 (2)在 Shape 类基础上设计圆形、矩形、三角形和梯形四个子类,要求根据实 际形状重写 getArea()方法。 (3)设计一个 TestShape 类,包含变量 area(存储总面积)、静态方法 countArea(Shapes),该方法负责把参数中的形状面积加入到 area 中。在 main 方 法中新建(2)中四种类型的对象 shape1、shape2、shape3、shape4,通过调用 countArea 方法把四个对象面积累加到 area 中,最后输出 area。

这个TestShape类和主函数怎么写啊?
...全文
736 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_45351181 2020-07-04
  • 打赏
  • 举报
回复
引用 1 楼 winner245的回复:
public class TestShape {
    public static void main(String[] args) {
        Shape shape1 = new Circle(10);
        Shape shape2 = new Rectangle(5,10); 
        Shape shape3 = new Triangle(1,2,3); // a, b, c
        Shape shape4 = new Trapezoid(3, 5, 4);  // a, b, h

        double area = countArea(shape1, shape2, shape3, shape4);
        System.out.println(area);
    }

    private static double countArea(Shape...shapes) {
        double area = 0.0;
        for (Shape shape : shapes)  area += shape.getArea();
        return area;
    }
}

abstract class Shape {
    public abstract double getArea();
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double getArea() {
        return length*width;
    }
}

class Triangle extends Shape {
    private double a;
    private double b;
    private double c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getArea() {
        double p = (a + b + c) / 2;
        return Math.sqrt(p * (p-a) * (p-b) * (p-c));
    }
}

class Trapezoid extends Shape {
    private double a;
    private double b;
    private double h;

    public Trapezoid(double a, double b, double h) {
        this.a = a;
        this.b = b;
        this.h = h;
    }

    @Override
    public double getArea() {
        return (a + b) * h / 2;
    }
}
蟹蟹,能问下代码中for的作用吗?
qq_45351181 2020-07-04
  • 打赏
  • 举报
回复
引用 1 楼 winner245的回复:
public class TestShape {
    public static void main(String[] args) {
        Shape shape1 = new Circle(10);
        Shape shape2 = new Rectangle(5,10); 
        Shape shape3 = new Triangle(1,2,3); // a, b, c
        Shape shape4 = new Trapezoid(3, 5, 4);  // a, b, h

        double area = countArea(shape1, shape2, shape3, shape4);
        System.out.println(area);
    }

    private static double countArea(Shape...shapes) {
        double area = 0.0;
        for (Shape shape : shapes)  area += shape.getArea();
        return area;
    }
}

abstract class Shape {
    public abstract double getArea();
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double getArea() {
        return length*width;
    }
}

class Triangle extends Shape {
    private double a;
    private double b;
    private double c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getArea() {
        double p = (a + b + c) / 2;
        return Math.sqrt(p * (p-a) * (p-b) * (p-c));
    }
}

class Trapezoid extends Shape {
    private double a;
    private double b;
    private double h;

    public Trapezoid(double a, double b, double h) {
        this.a = a;
        this.b = b;
        this.h = h;
    }

    @Override
    public double getArea() {
        return (a + b) * h / 2;
    }
}
蟹蟹,能问下代码中for的作用吗?
winner245 2020-07-04
  • 打赏
  • 举报
回复
for 的作用是循环,就是对每个shape都分别计算其面积,并累加
winner245 2020-07-03
  • 打赏
  • 举报
回复
public class TestShape {
    public static void main(String[] args) {
        Shape shape1 = new Circle(10);
        Shape shape2 = new Rectangle(5,10); 
        Shape shape3 = new Triangle(1,2,3); // a, b, c
        Shape shape4 = new Trapezoid(3, 5, 4);  // a, b, h

        double area = countArea(shape1, shape2, shape3, shape4);
        System.out.println(area);
    }

    private static double countArea(Shape...shapes) {
        double area = 0.0;
        for (Shape shape : shapes)  area += shape.getArea();
        return area;
    }
}

abstract class Shape {
    public abstract double getArea();
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double getArea() {
        return length*width;
    }
}

class Triangle extends Shape {
    private double a;
    private double b;
    private double c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getArea() {
        double p = (a + b + c) / 2;
        return Math.sqrt(p * (p-a) * (p-b) * (p-c));
    }
}

class Trapezoid extends Shape {
    private double a;
    private double b;
    private double h;

    public Trapezoid(double a, double b, double h) {
        this.a = a;
        this.b = b;
        this.h = h;
    }

    @Override
    public double getArea() {
        return (a + b) * h / 2;
    }
}

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧