Skip to content

Instantly share code, notes, and snippets.

@ifucolo
Created January 28, 2017 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifucolo/277e5e8f473eadbb91c4141b2a52f4b6 to your computer and use it in GitHub Desktop.
Save ifucolo/277e5e8f473eadbb91c4141b2a52f4b6 to your computer and use it in GitHub Desktop.
public class Rectangle {
private double length;
private double height;
// getters/setters ...
}
public class Circle {
private double radius;
// getters/setters ...
}
public class AreaFactory {
public double calculateArea(ArrayList<Object>... shapes) {
double area = 0;
for (Object shape : shapes) {
if (shape instanceof Rectangle) {
Rectangle rect = (Rectangle)shape;
area += (rect.getLength() * rect.getHeight());
} else if (shape instanceof Circle) {
Circle circle = (Circle)shape;
area +=
(circle.getRadius() * cirlce.getRadius() * Math.PI);
} else {
throw new RuntimeException("Shape not supported");
}
}
return area;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment