fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. interface Calc{
  6. int apply(int x, int y);
  7. }
  8.  
  9. class Add implements Calc{
  10. public int apply(int x, int y){
  11. return x + y;
  12. }
  13. }
  14. class Sub implements Calc{
  15. public int apply(int x, int y){
  16. return x - y;
  17. }
  18. }
  19. class Mul implements Calc{
  20. public int apply(int x, int y){
  21. return x * y;
  22. }
  23. }
  24. class Div implements Calc{
  25. public int apply(int x, int y){
  26. return x / y;
  27. }
  28. }
  29.  
  30. public class Main {
  31. public static void main(String[] args) {
  32. List<Calc> calcs = new ArrayList<>();
  33. calcs.add(new Add());
  34. calcs.add(new Sub());
  35. calcs.add(new Mul());
  36. calcs.add(new Div());
  37.  
  38. int x = 8;
  39. int y = 4;
  40.  
  41. for(Calc c: calcs){
  42. int result = c.apply(x, y);
  43. System.out.println(result);
  44. }
  45.  
  46. }
  47.  
  48. }
Success #stdin #stdout 0.11s 54656KB
stdin
Standard input is empty
stdout
12
4
32
2