fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. interface Shape{
  8. void draw();
  9. }
  10.  
  11. class Rectangle implements Shape{
  12. public void draw(){
  13. System.out.println("Drawing Rectangle.");
  14. }
  15. }
  16.  
  17. class Triangle implements Shape{
  18. public void draw(){
  19. System.out.println("Drawing Triangle.");
  20. }
  21. }
  22.  
  23. class Circle implements Shape{
  24. public void draw(){
  25. System.out.println("Drawing Circle.");
  26. }
  27. }
  28.  
  29. class RandomShapeFactory{
  30. public static Shape createRandomShape(){
  31.  
  32. Shape randomShape;
  33.  
  34. Random random = new Random();
  35. int randomNo = random.nextInt() % 3 + 1;
  36. if (randomNo == 1){
  37. randomShape = new Rectangle();
  38. }
  39. else if (randomNo == 2){
  40. randomShape = new Triangle();
  41. }
  42. else{
  43. randomShape = new Circle();
  44. }
  45.  
  46. return randomShape;
  47. }
  48. }
  49.  
  50. class Main{
  51. public static void main(String[] args){
  52. Shape[] shapes = new Shape[10];
  53. for (int i = 0; i < shapes.length; i++){
  54. shapes[i] = RandomShapeFactory.createRandomShape();
  55. }
  56. drawAllShapes(shapes);
  57. }
  58.  
  59. public static void drawAllShapes(Shape[] shapes){
  60. for (int i = 0; i < shapes.length; i++){
  61. shapes[i].draw();
  62. }
  63. }
  64. }
Success #stdin #stdout 0.03s 711168KB
stdin
Standard input is empty
stdout
Drawing Triangle.
Drawing Triangle.
Drawing Rectangle.
Drawing Circle.
Drawing Circle.
Drawing Circle.
Drawing Rectangle.
Drawing Rectangle.
Drawing Circle.
Drawing Rectangle.