Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Date;
- class CoordinateGenerator
- {
- private Random randomGenerator;
- public CoordinateGenerator()
- {
- Date now = new Date();
- long sec = now.getTime();
- randomGenerator = new Random(sec);
- }
- public int generateX()
- {
- int x = randomGenerator.nextInt(101);
- if(x < 5) {x = 0;}
- else if(x > 95) {x = 100;}
- else {x = randomGenerator.nextInt(99) + 1;}
- return x;
- }
- public int generateY()
- {
- int y = randomGenerator.nextInt(101);
- if(y < 5) {y = 0;}
- else if(y > 95) {y = 50;}
- else {y = randomGenerator.nextInt(49) + 1;}
- return y;
- }
- }
- class Out extends Exception
- {
- public Out()
- {
- super("Mingea a mers in Out!");
- }
- }
- class Gol extends Exception
- {
- public Gol()
- {
- super("S-a marcat un Gol!");
- }
- }
- class Corner extends Exception
- {
- public Corner()
- {
- super("Lovitura de Corner!");
- }
- }
- class Minge
- {
- private int x,y;
- public Minge(int X,int Y)
- {
- x=X;
- y=Y;
- }
- public int getX()
- {
- return x;
- }
- public int getY()
- {
- return y;
- }
- public void suteaza() throws Out,Gol,Corner
- {
- CoordinateGenerator x_nou=new CoordinateGenerator();
- CoordinateGenerator y_nou=new CoordinateGenerator();
- x=x_nou.generateX();
- y=y_nou.generateY();
- if(x==0||y==50)
- throw new Out();
- else if((x==0||x==100)&&(y>=20&&y<=30))
- throw new Gol();
- else if((x==0||x==100)&&(((0<y)&&(y<20))||((30<y)&&(y<50))))
- throw new Corner();
- }
- }
- class Joc
- {
- private String nume_echipa1,nume_echipa2;
- private int numar_goluri1,numar_goluri2;
- private int numar_out,numar_corner;
- public Joc(String n1,String n2)
- {
- nume_echipa1=n1;
- nume_echipa2=n2;
- }
- public String toString()
- {
- String s="";
- s=s+nume_echipa1+"-"+nume_echipa2+" "+numar_goluri1+"-"+numar_goluri2+" ";
- s=s+"numar outuri:"+numar_out+";"+"numar cornere:"+numar_corner;
- return s;
- }
- public void simuleaza() throws Gol,Out,Corner
- {
- int i;
- Minge m=new Minge(50,25);
- System.out.println(nume_echipa1+"-"+nume_echipa2+":Mingea se afla la coordonatele ("+m.getX()+","+m.getY()+")");
- try
- {
- for(i=0;i<1000;i++)
- {
- m.suteaza();
- }
- }catch(Gol e)
- {
- Minge m_nou=new Minge(50,25);
- }catch(Out e)
- {
- Minge m_nou=new Minge(m.getX(),m.getY());
- }catch(Corner e)
- {
- }finally
- {
- System.out.println(nume_echipa1+"-"+nume_echipa2+":Mingea se afla la coordonatele ("+m.getX()+","+m.getY()+")");
- }
- }
- }
- class Main
- {
- public static void main(String argv[])
- {
- Joc j=new Joc("Liverpool","Chelsea");
- j.simuleaza();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement