Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. interface Shape {}
  2.  
  3. class Rectangle implements Shape {
  4. double x, y;
  5. }
  6.  
  7. class Circle implements Shape {
  8. double radius;
  9. }
  10.  
  11. static double circumference(Shape shape) {
  12. if (shape instanceof Rectangle) {
  13. Rectangle rectangle = (Rectangle) shape;
  14. return (rectangle.x + rectangle.y) * 2;
  15. } else if (shape instanceof Circle) {
  16. Circle circle = (Circle) shape;
  17. return 2 * PI * circle.radius;
  18. } else {
  19. throw new UnsupportedOperationException();
  20. }
  21. }
  22.  
  23. static double area(Shape shape) {
  24. if (shape instanceof Rectangle) {
  25. Rectangle rectangle = (Rectangle) shape;
  26. return rectangle.x * rectangle.y;
  27. } else if (shape instanceof Circle) {
  28. Circle circle = (Circle) shape;
  29. return PI * circle.radius * circle.radius;
  30. } else {
  31. throw new UnsupportedOperationException();
  32. }
  33. }
  34.  
  35. static Shape longestLine(Shape shape) {
  36. if (shape instanceof Rectangle) {
  37. Rectangle rectangle = (Rectangle) shape;
  38. return Math.sqrt(rectangle.x * rectangle.x + rectangle.y * rectangle.y);
  39. } else if (shape instanceof Circle) {
  40. Circle circle = (Circle) shape;
  41. return 2 * circle.radius;
  42. } else {
  43. throw new UnsupportedOperationException();
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement