Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement