Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package soccerexceptions; //asta e vrajeala din eclipse se pune cand creezi o clasa noua
  2.  
  3. import java.util.Random;  //pachetul asta trebuie importat ca ai nevoie de el ca sa dea numere random
  4.  
  5. public class Ball {
  6.    
  7.     //Coordonatele de inceput ca sa initializezi obiectul nou creat, cand creezi obiectul o sa fie (50,50)
  8.     //pentru ca se incepe meciul de la mijloc
  9.     int x;          
  10.     int y;
  11.    
  12.     //Variabila de care te folosesti ca sa obtii numere random
  13.     Random r;
  14.    
  15.     //Constructor
  16.     public Ball(int x,int y) {
  17.         this.x = x;
  18.         this.y = y;
  19.     }
  20.    
  21.     //Getter ca sa iei X-ul
  22.     public int GetX() {
  23.         return x;
  24.     }
  25.    
  26.     //Getter ca sa iei Y-ul
  27.     public int GetY() {
  28.         return y;
  29.     }
  30.    
  31.     //Metoda ca sa incepi jocul
  32.     public void Play() throws OutException, GoalException, CornerException {
  33.  
  34.         //Initializezi variabila de ti ai declarat o mai sus ca fiind un obiect de tipul clasei Random
  35.         //e deja o clasa implementat default cand apelezi pachetul ala de sus de tot
  36.         r = new Random();
  37.        
  38.         //aici egalez x,y cu r.metodaCareDaNumarRandom si anume r.nextInt() si am pus in paranteza 101 si
  39.         //51 ca sa dea numere de la 0 la 100 si numere de la 0 la 50 ca altfel daca puneam 100 lua numere
  40.         //de la 0 la 99
  41.         x = r.nextInt(101);
  42.         y = r.nextInt(51);
  43.        
  44.         //Aici printez unde e mingea dupa ce se baga random un X si un Y
  45.         System.out.println("Ball:("+x+","+y+")");
  46.        
  47.        
  48.         //Tot ce e in jos e ce iti apare in enuntul problemei
  49.         if( y == 0 || y == 50) {
  50.             throw new OutException();
  51.         }
  52.         else {
  53.             if( (x == 0 || x == 100) &&  (y>=20 && y<=30) ) {
  54.                 throw new GoalException();
  55.             }
  56.             else {
  57.                 if( (x == 0 || x == 100) && ( ((y>0)&&(y<20)) ||  ((y>30)&&(y<50))  ) )  {
  58.                     throw new CornerException();
  59.                 }
  60.             }
  61.         }
  62.        
  63.        
  64.     }
  65. }
  66.  
  67.  
  68. package soccerexceptions;
  69.  
  70.  
  71. //Clasa asta si celelalte asa cu numele Exception extind clasa mare Exception
  72. //si suprascriu in constructor numele exceptiei
  73. public class CornerException extends Exception{
  74.  
  75.     public CornerException() {
  76.         super("CornerException");
  77.     }
  78.    
  79. }
  80.  
  81.  
  82. package soccerexceptions;
  83.  
  84.  
  85.  
  86. //Clasa asta si celelalte asa cu numele Exception extind clasa mare Exception
  87. //si suprascriu in constructor numele exceptiei
  88. public class OutException extends Exception{
  89.  
  90.     public OutException() {
  91.         super("OutException");
  92.     }
  93.    
  94. }
  95.  
  96.  
  97.  
  98. package soccerexceptions;
  99.  
  100.  
  101.  
  102. //Clasa asta si celelalte asa cu numele Exception extind clasa mare Exception
  103. //si suprascriu in constructor numele exceptiei
  104. public class GoalException extends Exception{
  105.  
  106.     public GoalException() {
  107.         super("GoalException");
  108.     }
  109.    
  110. }
  111.  
  112.  
  113. package soccerexceptions;
  114.  
  115. public class Game {
  116.  
  117.    
  118.     //Primele doua variabile numele echipelor, ultimele 3 contoare practic pentru ce zice fiecare
  119.     String teamOne;
  120.     String teamTwo;
  121.     int outs;
  122.     int goals;
  123.     int corners;
  124.    
  125.     //Constructor
  126.     public Game(String teamOne, String teamTwo) {
  127.         this.teamOne = teamOne;
  128.         this.teamTwo = teamTwo;
  129.     }
  130.    
  131.     //Metoda ca sa incepem si noi meciul ca pe arena electrica
  132.     public void StartGame() {
  133.        
  134.         //Cum am zis si in clasa Ball, initializezi mingea cu 50,50 ca incepe de la mijloc
  135.         Ball B = new Ball(50,50);
  136.        
  137.         //Aici iti zice in enunt ca pentru 2000 de cazuri sa faci
  138.         for(int i = 0 ; i<2000 ; i++) {
  139.            
  140.             //In variabilele astea retin mereu ultimele coordonate
  141.             int lastX = B.GetX();
  142.             int lastY = B.GetY();
  143.            
  144.             //De aici incepe show-ul
  145.             try {
  146.                 B.Play();                 //Executam metoda Play din clasa Ball de la care mereu obtinem
  147.                                           //coordonate noi si ai if-urile alea daca se indeplineste unul
  148.                                           //se "arunca" o exceptie si mai jos ai ce se intampla cand se
  149.                                           //"arunca" adica "throw" fiecare exceptie
  150.             }catch(GoalException e) {
  151.                 goals++;
  152.                 B = new Ball(50,25); //Daca e gol se reincepe de la mijloc adica X e 50 si Y e 25
  153.                 System.out.println(e);
  154.             }catch(OutException e) {
  155.                 outs++;
  156.                 B = new Ball(lastX,lastY); // Daca e out se creeaza un nou obiect si se reia de unde a iesit adica de la
  157.                                            // ultimul X si ultimul Y
  158.                 System.out.println(e);
  159.                 System.out.println("Se executa outul de la:("+lastX+","+lastY+")");
  160.             }catch(CornerException e) {
  161.                 corners++;
  162.                 B = new Ball(lastX,lastY); //Aceeasi poveste ca mai sus;
  163.                 System.out.println(e);
  164.                 System.out.println("Se executa cornerul de la:("+lastX+","+lastY+")");
  165.             }
  166.         }
  167.     }
  168.    
  169.     //o metoda de toString aici intelegi tu ce se intampla
  170.     public String toString() {
  171.         String s = "";
  172.         s = s+"Team1:"+teamOne+"\n";
  173.         s = s+"Team2:"+teamTwo+"\n";
  174.         s = s+"Goals:"+goals+"\n";
  175.         s = s+"Outs:"+outs+"\n";
  176.         s = s+"Corners:"+corners+"\n";
  177.    
  178.         return s;
  179.     }
  180.    
  181.    
  182. }
  183.  
  184.  
  185. package soccerexceptions;
  186.  
  187. public class Main {
  188.    
  189.     public static void main(String [] args) {
  190.        
  191.         //Creez un nou obiect de tip Game
  192.             Game G = new Game("Electrica","FC Barcelona");
  193.         //Apelez metoda din clasa Game;
  194.             G.StartGame();
  195.             System.out.println(G);
  196.     }
  197.    
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement