Sum of the areas

Report a typo

You are given the following classes:

class Shape {
}

class Square extends Shape {
    private int side;

    public int getSide() {
        return side;
    }

    public void setSide(int side) {
        this.side = side;
    }
}

class Rectangle extends Shape {
    private int width, height;

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

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

Unfortunately, the author of this class hierarchy forgot to add the getArea() method to these classes. Now you need to implement a method that calculates the sum of areas of the Shape array. If some elements are instances of the class Shape, their area equals 0.

Write a program in Java 17
class Sum {
public static int sumOfAreas(Shape[] array) {
// write your code here
}
}

//Don't change the code below
class Shape {
}

class Square extends Shape {
private int side;

public int getSide() {
return side;
}

public void setSide(int side) {
this.side = side;
}
}

class Rectangle extends Shape {
private int width;
private int height;

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
___

Create a free account to access the full topic