Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Date;
  3.  
  4. class Out extends Exception{
  5. String mesaj;
  6. public Out(String mesaj) {
  7. super(mesaj);
  8. this.mesaj=mesaj;
  9. }
  10. public String toString() {
  11. return mesaj;
  12. }
  13. }
  14.  
  15. class Gol extends Exception{
  16. String mesaj;
  17. public Gol(String mesaj) {
  18. super(mesaj);
  19. this.mesaj=mesaj;
  20. }
  21. public String toString() {
  22. return mesaj;
  23. }
  24. }
  25.  
  26. class Corner extends Exception{
  27. String mesaj;
  28. public Corner(String mesaj) {
  29. super(mesaj);
  30. this.mesaj=mesaj;
  31. }
  32. public String toString() {
  33. return mesaj;
  34. }
  35. }
  36. class CoordinateGenerator
  37. {
  38. private Random randomGenerator;
  39. public CoordinateGenerator()
  40. {
  41. Date now = new Date();
  42. long sec = now.getTime();
  43. randomGenerator = new Random(sec);
  44. }
  45. public int generateX()
  46. {
  47. int x = randomGenerator.nextInt(101);
  48. if(x < 5)
  49. {
  50. x = 0;
  51. }
  52. else if(x > 95)
  53. {
  54. x = 100;
  55. }
  56.  
  57. else
  58. {
  59. x = randomGenerator.nextInt(99) + 1;
  60. }
  61. return x;
  62. }
  63. public int generateY()
  64. {
  65. int y = randomGenerator.nextInt(101);
  66. if(y < 5)
  67. {
  68. y = 0;
  69. }
  70. else if(y > 95)
  71. {
  72. y = 50;
  73. }
  74. else
  75. {
  76. y = randomGenerator.nextInt(49) + 1;
  77. }
  78. return y;
  79. }
  80. }
  81.  
  82.  
  83.  
  84. class Minge
  85. {
  86. private double X,Y;
  87.  
  88. public Minge()
  89. {
  90. CoordinateGenerator gen = new CoordinateGenerator();
  91. X = gen.generateX();
  92. Y = gen.generateY();
  93. }
  94. public Minge(double X, double Y) {
  95. this.X = X;
  96. this.Y = Y;
  97. }
  98.  
  99. public double getX()
  100. {
  101. return X;
  102. }
  103.  
  104. public double getY()
  105. {
  106. return Y;
  107. }
  108.  
  109. public void suteaza() throws Out,Gol,Corner
  110. {
  111. CoordinateGenerator gen = new CoordinateGenerator();
  112. X = gen.generateX();
  113. Y = gen.generateY();
  114.  
  115. if (Y == 0 || Y == 50)
  116. throw new Out("Out!\n");
  117. if ((X == 0 || X == 100) && (Y>= 20 && Y<=30))
  118. throw new Gol("GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOL ");
  119. if ((X == 0 || X == 100) && ((Y > 0 && Y < 20) || (Y > 30 && Y < 50)))
  120. throw new Corner("Corner!\n");
  121. }
  122.  
  123. public String toString() {
  124. return "(" + X + "," + Y + ")" + "\n";
  125. }
  126. }
  127.  
  128. class Joc{
  129. private String e1, e2;
  130. private int gol1, gol2, out, corner;
  131.  
  132. public Joc(String e1, String e2) {
  133. this.e1=e1;
  134. this.e2=e2;
  135. }
  136.  
  137. public String toString() {
  138. return e1 + " " + gol1 + " - " + gol2 + " " + e2 + " " + out + " " + corner + "\n";
  139. }
  140.  
  141. public void simuleaza() {
  142. Minge minge = new Minge();
  143.  
  144.  
  145. for (int i=0;i<1000;i++) {
  146. try {
  147. minge.suteaza();
  148. } catch (Gol exceptie){
  149. if (minge.getX() == 0) {
  150. gol2++;
  151. System.out.println(exceptie + e2 + "\n");
  152. }
  153. else {
  154. gol1++;
  155. System.out.println(exceptie + e1 + "\n");
  156. }
  157. minge = new Minge(50, 25);
  158. }
  159. catch (Corner exceptie) {
  160. corner++;
  161. System.out.println(exceptie);
  162. if (minge.getX() == 0)
  163. minge = new Minge(0,0);
  164. else
  165. minge = new Minge (100,50);
  166. }
  167. catch (Out exceptie) {
  168. System.out.println(exceptie);
  169. minge = new Minge(minge.getX(),minge.getY());
  170. out++;
  171. }
  172. finally {
  173. System.out.println(e1 + " - " + e2 + ": Mingea se afla in coordonatele " + minge);
  174. }
  175.  
  176. }
  177. }
  178.  
  179.  
  180.  
  181. }
  182. public class Exceptii4
  183. {
  184. public static void main (String[] args)
  185. {
  186. Joc joc1 = new Joc ("Steaua","Dinamo"), joc2 = new Joc ("FC Ineu","Real Madrid");
  187. joc1.simuleaza();
  188. System.out.println(joc1);
  189.  
  190. joc2.simuleaza();
  191. System.out.println(joc2);
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement