fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.stream.*;
  4. import java.util.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. interface Figure {
  9.  
  10. double square();
  11. }
  12.  
  13. final class Rectangle implements Figure {
  14.  
  15. private final double width;
  16. private final double height;
  17.  
  18. public Rectangle(final double width, final double height) {
  19. this.width = width;
  20. this.height = height;
  21. }
  22.  
  23. @Override
  24. public double square() {
  25. return width * height;
  26. }
  27.  
  28. @Override
  29. public String toString() {
  30. return String.format("%s{ width = %f ; height = %f }",
  31. getClass().getSimpleName(), width, height);
  32. }
  33. }
  34.  
  35. final class Circle implements Figure {
  36.  
  37. private final double radius;
  38.  
  39. public Circle(final double radius) {
  40. this.radius = radius;
  41. }
  42.  
  43. @Override
  44. public double square() {
  45. return Math.PI * radius * radius;
  46. }
  47.  
  48. @Override
  49. public String toString() {
  50. return String.format("%s{ radius = %f }",
  51. getClass().getSimpleName(), radius);
  52. }
  53. }
  54.  
  55. /* Name of the class has to be "Main" only if the class is public. */
  56. class Ideone {
  57.  
  58. private static void printSquares(final Figure... figures) {
  59. Stream.of(figures).forEach(f ->
  60. System.out.printf("%s square = %f%n", f.toString(), f.square()));
  61. }
  62.  
  63. public static void main (String[] args) throws java.lang.Exception {
  64. printSquares(new Rectangle(3, 4), new Circle(2));
  65. }
  66. }
Success #stdin #stdout 0.24s 320832KB
stdin
Standard input is empty
stdout
Rectangle{ width = 3.000000 ; height = 4.000000 } square = 12.000000
Circle{ radius = 2.000000 } square = 12.566371