Не удается найти символы при использовании абстрактных классов - PullRequest
0 голосов
/ 17 июня 2020

Практика абстрактных классов с фигурами. Цель состоит в том, чтобы получить общую площадь трех фигур с помощью абстрактных классов. Пока это то, что у меня есть.

Я не уверен, правильно ли я сделал эту часть:

    static double sumArea(Shape[] arr){
        // Sum up the areas of all the shapes using getArea()
        return arr.getArea();
    }

Я все время получаю сообщение об ошибке, что он не находит символ h (высота), w (ширина), tw (верхняя_ширина). Кто-нибудь знает, почему он не находит эти символы?


public class TestShape{
    public static void main(String args[]){
        Point p = new Point(1, 1);

        Shape[] arr = {
            new Rectangle(p, 3, 4),
            new Parallelogram(p, 5, 6, Math.PI/6.0),
            new Trapezoid(p, 5, 6, 2)
        };

        System.out.println("SUM_AREA = " + sumArea(arr));
    }

    static double sumArea(Shape[] arr){
        // Sum up the areas of all the shapes using getArea()
        return arr.getArea();
    }
}

class Point{
    double x, y;

    Point(){
        this(0, 0);
    }

    Point(double x, double y){
        this.x = x;
        this.y = y;
    }

    public String toString(){
        return "[" + x + ", " + y + "]";
    }
}

abstract class Shape{
    Shape(){

    }

    Shape(Point p){ 
        this.p = p;
    }

    public Point getPosition(){
        return p; 
    }

    public void setPosition(Point p){
        this.p = p;
    }

    // Abstract method
    public abstract double getArea(); 
}

abstract class Quadrangle extends Shape{
    protected double width, height;

    Quadrangle(Point p, double w, double h){
        this.p = p;
        this.width = w;
        this.height = h;
    }

    public double getWidth(){
        return w;
    }

    public double getHeight(){
        return h;
    }

    public void setWidth(double w){
        this.weight = w;
    }

    public void setHeight(double h){
        this.height = h;
    }
}

class Rectangle extends Quadrangle{
    Rectangle(Point p, double w, double h){
        this.p = p;
        this.width = w;
        this.height = h;
    }

    public boolean isSquare(){
        if(w == h){
            return "Error";
        }
    }

    @Override /** Return Area */
    public double getArea(){
        return w * h;
    }
}

class Parallelogram extends Quadrangle{
    protected double angle;

    Parallelogram(Point p, double w, double h, double angle){
        this.p = p;
        this.weight = w;
        this.height = h;
        this.angle = angle;
    }

    public double getAngle(){
        return angle;
    }

    public void setAngle(double a){
        this.angle = a;
    }

    @Override /** Return Area */
    public double getArea(){
        return w * h;
    }
}

class Trapezoid extends Quadrangle{
    protected double top_width;

    Trapezoid(Point p, double w, double h, double top_width){
        this.p = p;
        this.width = w;
        this.height = h;
        this.top_width = top_width;
    }

    public double getTopWidth(){
        return top_width;
    }

    public void setTopWidth(double tw){
        this.top_width = tw;
    }

    @Override /** Return Area */
    public double getArea(){
        return ((w + tw) / 2) * h;
    }
}

1 Ответ

1 голос
/ 17 июня 2020

Имена w, tw и так далее существуют только как параметры. Если вы хотите получить доступ к значениям, которые вы сохраняете в конструкторах, вы должны использовать имя левой стороны: this.[width or whatever].

Кроме того, перепишите sumArea примерно так:

static double sumArea(Shape[] arr){
        // Sum up the areas of all the shapes using getArea()
        double totalArea = 0;
        for (Shape shape : arr) {
                totalArea += shape.getArea();
        }
        return totalArea;
}
...