Andrei_M

Exceptions

Dec 4th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. import java.util.Random;
  2. class Out extends Exception
  3. {
  4.     public Out()
  5.     {
  6.        
  7.         super("Out");
  8.     }
  9. }
  10.  
  11. class Goal extends Exception
  12. {
  13.     public Goal()
  14.     {
  15.         super("Goal");
  16.     }
  17. }
  18.  
  19. class Corner extends Exception
  20. {
  21.     public Corner()
  22.     {
  23.         super("Corner");
  24.     }
  25. }
  26. //-----------------------------------------------------------------------------------------
  27. class Ball
  28. {
  29.     private int x, y;
  30.     Random r = new Random();
  31.    
  32.     public Ball(int x, int y)
  33.     {
  34.         this.x = x;
  35.         this.y = y;
  36.     }
  37.    
  38.     public void play() throws Out, Corner, Goal
  39.     {
  40.         int check1 = 0, check2 = 0;
  41.         this.x = r.nextInt(101);
  42.         this.y = r.nextInt(51);
  43.        
  44.         if( y == 0 || y == 50)
  45.         {
  46.             check1 = 1;
  47.             throw new Out();
  48.            
  49.         }
  50.         if(( x == 0 || x == 100) && ( y >= 20 && y <= 30))
  51.         {
  52.             check2 = 1;
  53.             throw new Goal();
  54.            
  55.         }
  56.         if((x == 0 || x == 100) && check1 == 0 && check2 == 0)
  57.         {
  58.             throw new Corner();
  59.         }
  60.     }
  61.    
  62.     public int get_x()
  63.     {
  64.         return this.x;
  65.     }
  66.    
  67.     public int get_y()
  68.     {
  69.         return this.y;
  70.     }
  71.    
  72.     public String toString()
  73.     {
  74.         return "Ball at " + this.x + " " + this.y + "\n";
  75.     }
  76. }
  77.  
  78. //-----------------------------------------------------------------------------------------
  79.  
  80. class Game
  81. {
  82.     private String team1, team2;
  83.     private int outs = 0, goals = 0, corners = 0;
  84.    
  85.     int prev_x = 1, prev_y = 1;
  86.     Ball ball = new Ball(prev_x,prev_y);
  87.    
  88.     int goals_1 = 0, goals_2 = 0;
  89.    
  90.     public Game(String team1, String team2)
  91.     {
  92.         this.team1 = team1;
  93.         this.team2 = team2;
  94.     }
  95.    
  96.     public String toString()
  97.     {
  98.        
  99.         return team1 + " - " + team2 + "\n" + goals + " Goals\n" + outs+ " Outs\n" + corners + " Corners\n";  
  100.     }
  101.    
  102.    
  103.     public String simulate()
  104.     {
  105.         for(int i = 0; i < 2000; i++)
  106.         {
  107.            
  108.            
  109.             try
  110.             {
  111.                 prev_x = ball.get_x();
  112.                 prev_y = ball.get_y();
  113.                
  114.                 ball.play();
  115.                 System.out.println(ball);
  116.                
  117.             }
  118.             catch(Out e)
  119.             {
  120.                 outs++;
  121.                 System.out.println(e.getMessage());
  122.                 ball = new Ball(prev_x, prev_y);
  123.                
  124.             }
  125.             catch(Corner e)
  126.             {
  127.                 corners++;
  128.                 System.out.println(e.getMessage());
  129.                 if(ball.get_x() <= 50)
  130.                     if(ball.get_y() <= 25)
  131.                         ball = new Ball(0,0);
  132.                     else
  133.                         ball = new Ball(0, 50);
  134.                 else
  135.                 {
  136.                     if(ball.get_y() <= 25)
  137.                         ball = new Ball(100,0);
  138.                     else
  139.                         ball = new Ball(100, 50);
  140.                 }
  141.                
  142.             }
  143.             catch(Goal e)
  144.             {
  145.                 goals++;
  146.                 System.out.println(e.getMessage());
  147.                 if(ball.get_x() == 0 && (ball.get_y() >= 20 && ball.get_y() <= 30))
  148.                     goals_1++;
  149.                 else if(ball.get_x() == 100 && (ball.get_y() >= 20 && ball.get_y() <= 30))
  150.                     goals_2++;
  151.                 ball = new Ball(50, 25);
  152.             }
  153.         }
  154.         String team_goals = team1 + " = " + goals_1+ " " + team2 + " = " + goals_2 + "\n";
  155.         return team_goals;
  156.     }
  157. }
  158.  
  159. //-----------------------------------------------------------------------------------------
  160. class Main
  161. {
  162.     public static void main(String[] args)
  163.     {
  164.         Game game = new Game("Team 1", "Team 2");
  165.         System.out.println(game.simulate());
  166.        
  167.         System.out.println(game);
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment