Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Date;
  3. class CoordinateGenerator
  4. {
  5. private Random randomGenerator;
  6. public CoordinateGenerator()
  7. {
  8. Date now = new Date();
  9. long sec = now.getTime();
  10. randomGenerator = new Random(sec);
  11. }
  12. public int generateX()
  13. {
  14. int x = randomGenerator.nextInt(101);
  15. if(x < 5) {x = 0;}
  16. else if(x > 95) {x = 100;}
  17. else {x = randomGenerator.nextInt(99) + 1;}
  18. return x;
  19. }
  20. public int generateY()
  21. {
  22. int y = randomGenerator.nextInt(101);
  23. if(y < 5) {y = 0;}
  24. else if(y > 95) {y = 50;}
  25. else {y = randomGenerator.nextInt(49) + 1;}
  26. return y;
  27. }
  28. }
  29. class Out extends Exception
  30. {
  31. public Out()
  32. {
  33. super("Mingea a mers in Out!");
  34. }
  35. }
  36. class Gol extends Exception
  37. {
  38. public Gol()
  39. {
  40. super("S-a marcat un Gol!");
  41. }
  42. }
  43. class Corner extends Exception
  44. {
  45. public Corner()
  46. {
  47. super("Lovitura de Corner!");
  48. }
  49. }
  50.  
  51. class Minge
  52. {
  53. private int x,y;
  54. public Minge(int X,int Y)
  55. {
  56. x=X;
  57. y=Y;
  58. }
  59. public int getX()
  60. {
  61. return x;
  62. }
  63. public int getY()
  64. {
  65. return y;
  66. }
  67. public void suteaza() throws Out,Gol,Corner
  68. {
  69. CoordinateGenerator x_nou=new CoordinateGenerator();
  70. CoordinateGenerator y_nou=new CoordinateGenerator();
  71. x=x_nou.generateX();
  72. y=y_nou.generateY();
  73. if(x==0||y==50)
  74. throw new Out();
  75. else if((x==0||x==100)&&(y>=20&&y<=30))
  76. throw new Gol();
  77. else if((x==0||x==100)&&(((0<y)&&(y<20))||((30<y)&&(y<50))))
  78. throw new Corner();
  79. }
  80. }
  81. class Joc
  82. {
  83. private String nume_echipa1,nume_echipa2;
  84. private int numar_goluri1,numar_goluri2;
  85. private int numar_out,numar_corner;
  86. public Joc(String n1,String n2)
  87. {
  88. nume_echipa1=n1;
  89. nume_echipa2=n2;
  90. }
  91. public String toString()
  92. {
  93. String s="";
  94. s=s+nume_echipa1+"-"+nume_echipa2+" "+numar_goluri1+"-"+numar_goluri2+" ";
  95. s=s+"numar outuri:"+numar_out+";"+"numar cornere:"+numar_corner;
  96. return s;
  97. }
  98. public void simuleaza() throws Gol,Out,Corner
  99. {
  100. int i;
  101. Minge m=new Minge(50,25);
  102. System.out.println(nume_echipa1+"-"+nume_echipa2+":Mingea se afla la coordonatele ("+m.getX()+","+m.getY()+")");
  103. try
  104. {
  105. for(i=0;i<1000;i++)
  106. {
  107. m.suteaza();
  108. }
  109. }catch(Gol e)
  110. {
  111. Minge m_nou=new Minge(50,25);
  112. }catch(Out e)
  113. {
  114. Minge m_nou=new Minge(m.getX(),m.getY());
  115. }catch(Corner e)
  116. {
  117.  
  118. }finally
  119. {
  120. System.out.println(nume_echipa1+"-"+nume_echipa2+":Mingea se afla la coordonatele ("+m.getX()+","+m.getY()+")");
  121. }
  122.  
  123.  
  124. }
  125.  
  126. }
  127. class Main
  128. {
  129. public static void main(String argv[])
  130. {
  131. Joc j=new Joc("Liverpool","Chelsea");
  132. j.simuleaza();
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement