Java

时间:2018-11-01 11:30:14

标签: java

我是Java新手,目前正在从事实习任务:

给出以下类型的图形对象:正方形,三角形,圆形,梯形。可以绘制每个形状,获取其面积和颜色。同样,这些图形具有独特的方法,例如:返回半径,斜边的长度,边的长度等。 我们需要生成一个随机的形状集合,该集合中的对象数量也不是事先知道的。 生成数组后,您需要显示我们拥有的对象的完整列表,例如:

绘图三角形,面积:8.56,斜边:6.20,颜色:红色

绘图正方形,面积:27.27,边长:5.22,颜色:蓝色

...等等。 有必要使用OOP原理来描述任务。

我需要的是一般建议,以使我的代码更清晰,更好,并且对随机数组部分也有帮助。这是我当前编写的代码:

形状界面:

public interface Shape {
    void draw();

    double getArea();

    ColorEnum getColor();
}

ColorEnum枚举类:

public enum ColorEnum {
    RED,
    ORANGE,
    YELLOW,
    GREEN,
    BLUE,
    INDIGO,
    VIOLET
}

广场课:

    import java.awt.*;

public class Square implements Shape {
    private double side;
    private Color color;

    public Square(Color color, double side) {
        this.color = color;
        this.side = side;
    }

    @Override
    public void draw() {
        System.out.println("Drawing square, area: " + String.format("%.2f", getArea()) + ", side length: " + String.format("%.2f", getSide()) + ", color: " + getColor());
    }

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

    @Override
    public Color getColor() {
        return color;
    }

    public double getSide() {
        return side;
    }
}

三角形类别:

public class Triangle implements Shape {
    private double side1, side2, side3;
    private ColorEnum color;

    public Triangle(ColorEnum color, double side1, double side2, double side3) {
        this.color = color;
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    @Override
    public void draw() {
        System.out.println("Drawing triangle, area: " + String.format("%.2f", getArea()) + ", hypotenuse: " + String.format("%.2f", getHypotenuse()) + ", color: " + getColor().name().toLowerCase());
    }

    @Override
    public double getArea() {
        double p = (side1 + side2 + side3) / 2;
        double s = Math.sqrt((p * (p - side1) * (p - side2) * (p - side3)));
        return s;
    }

    @Override
    public ColorEnum getColor() {
        return color;
    }

    public double getHypotenuse() {
        double hypotenuse = 0;
        double[] arr = {side1, side2, side3};

        for (double i : arr) {
            if (hypotenuse < i) hypotenuse = i;
        }
        return hypotenuse;
    }
}

圈子类别:

public class Circle implements Shape {
    private double radius;
    private ColorEnum color;

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

    @Override
    public void draw() {
        System.out.println("Drawing circle, area: " + String.format("%.2f", getArea()) + ", radius: " + String.format("%.2f", getRadius()) + ", color: " + getColor().name().toLowerCase());
    }

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

    @Override
    public ColorEnum getColor() {
        return color;
    }

    public double getRadius() {
        return radius;
    }
}

训练类:

public class Trapeze implements Shape {
    private double base1, base2, height;
    private ColorEnum color;

    public Trapeze(ColorEnum color, double base1, double base2, double height) {
        this.color = color;
        this.base1 = base1;
        this.base2 = base2;
        this.height = height;
    }

    @Override
    public void draw() {
        System.out.println("Drawing trapeze, area: " + String.format("%.2f", getArea()) + ", height: " + String.format("%.2f", getHeight()) + ", color: " + getColor().name().toLowerCase());
    }

    @Override
    public double getArea() {
        return (base1 + base2) / 2 * height;
    }

    @Override
    public ColorEnum getColor() {
        return color;
    }

    public double getHeight() {
        return height;
    }
}

现在返回随机数组部分。我需要带有随机参数的随机形状。如何很好地实现这样的东西?我应该使用工厂模式吗?我不小心在GitHub上找到了一个实现,你能告诉我它很好吗? https://github.com/Ligren/QaTestLab/blob/master/QaTestLab/src/Test/ShapeDrawing.javahttps://github.com/Ligren/QaTestLab/blob/master/QaTestLab/src/Test/Start.java

1 个答案:

答案 0 :(得分:0)

因此,我完成了此任务,而雇主批准了:) 您可以在GitHub上检查最终版本: https://github.com/Wonderio619/QATestTask