Advertisement
duc-phan

Untitled

Dec 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. public class Square {
  2.     public Point topLeft;
  3.     public double side;
  4. }
  5. public class Rectangle {
  6.     public Point topLeft;
  7.     public double height;
  8.     public double width;
  9. }
  10. public class Circle {
  11.     public Point center;
  12.     public double radius;
  13. }
  14. public class Geometry {
  15.     public final double PI = 3.141592653589793;
  16.     public double area(Object shape) throws NoSuchShapeException {
  17.         if (shape instanceof Square) {
  18.             Square s = (Square)shape;
  19.             return s.side * s.side;
  20.         }
  21.         else if (shape instanceof Rectangle) {
  22.             Rectangle r = (Rectangle)shape;
  23.             return r.height * r.width;
  24.         }
  25.         else if (shape instanceof Circle) {
  26.             Circle c = (Circle)shape;
  27.             return PI * c.radius * c.radius;
  28.         }
  29.         throw new NoSuchShapeException();
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement