Last active
August 7, 2019 18:34
-
-
Save fboeller/f1465df917baf854f7d6bc07dce7b96d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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