Skip to content

Instantly share code, notes, and snippets.

@fboeller
Last active August 7, 2019 18:34
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 fboeller/f1465df917baf854f7d6bc07dce7b96d to your computer and use it in GitHub Desktop.
Save fboeller/f1465df917baf854f7d6bc07dce7b96d to your computer and use it in GitHub Desktop.
interface Shape {}
class Rectangle implements Shape {
double x, y;
}
class Circle implements Shape {
double radius;
}
static double circumference(Shape shape) {
if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
return (rectangle.x + rectangle.y) * 2;
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
return 2 * PI * circle.radius;
} else {
throw new UnsupportedOperationException();
}
}
static double area(Shape shape) {
if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
return rectangle.x * rectangle.y;
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
return PI * circle.radius * circle.radius;
} else {
throw new UnsupportedOperationException();
}
}
static Shape longestLine(Shape shape) {
if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
return Math.sqrt(rectangle.x * rectangle.x + rectangle.y * rectangle.y);
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
return 2 * circle.radius;
} else {
throw new UnsupportedOperationException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment