Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Date;
- class Out extends Exception{
- String mesaj;
- public Out(String mesaj) {
- super(mesaj);
- this.mesaj=mesaj;
- }
- public String toString() {
- return mesaj;
- }
- }
- class Gol extends Exception{
- String mesaj;
- public Gol(String mesaj) {
- super(mesaj);
- this.mesaj=mesaj;
- }
- public String toString() {
- return mesaj;
- }
- }
- class Corner extends Exception{
- String mesaj;
- public Corner(String mesaj) {
- super(mesaj);
- this.mesaj=mesaj;
- }
- public String toString() {
- return mesaj;
- }
- }
- 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 Minge
- {
- private double X,Y;
- public Minge()
- {
- CoordinateGenerator gen = new CoordinateGenerator();
- X = gen.generateX();
- Y = gen.generateY();
- }
- public Minge(double X, double Y) {
- this.X = X;
- this.Y = Y;
- }
- public double getX()
- {
- return X;
- }
- public double getY()
- {
- return Y;
- }
- public void suteaza() throws Out,Gol,Corner
- {
- CoordinateGenerator gen = new CoordinateGenerator();
- X = gen.generateX();
- Y = gen.generateY();
- if (Y == 0 || Y == 50)
- throw new Out("Out!\n");
- if ((X == 0 || X == 100) && (Y>= 20 && Y<=30))
- throw new Gol("GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOL ");
- if ((X == 0 || X == 100) && ((Y > 0 && Y < 20) || (Y > 30 && Y < 50)))
- throw new Corner("Corner!\n");
- }
- public String toString() {
- return "(" + X + "," + Y + ")" + "\n";
- }
- }
- class Joc{
- private String e1, e2;
- private int gol1, gol2, out, corner;
- public Joc(String e1, String e2) {
- this.e1=e1;
- this.e2=e2;
- }
- public String toString() {
- return e1 + " " + gol1 + " - " + gol2 + " " + e2 + " " + out + " " + corner + "\n";
- }
- public void simuleaza() {
- Minge minge = new Minge();
- for (int i=0;i<1000;i++) {
- try {
- minge.suteaza();
- } catch (Gol exceptie){
- if (minge.getX() == 0) {
- gol2++;
- System.out.println(exceptie + e2 + "\n");
- }
- else {
- gol1++;
- System.out.println(exceptie + e1 + "\n");
- }
- minge = new Minge(50, 25);
- }
- catch (Corner exceptie) {
- corner++;
- System.out.println(exceptie);
- if (minge.getX() == 0)
- minge = new Minge(0,0);
- else
- minge = new Minge (100,50);
- }
- catch (Out exceptie) {
- System.out.println(exceptie);
- minge = new Minge(minge.getX(),minge.getY());
- out++;
- }
- finally {
- System.out.println(e1 + " - " + e2 + ": Mingea se afla in coordonatele " + minge);
- }
- }
- }
- }
- public class Exceptii4
- {
- public static void main (String[] args)
- {
- Joc joc1 = new Joc ("Steaua","Dinamo"), joc2 = new Joc ("FC Ineu","Real Madrid");
- joc1.simuleaza();
- System.out.println(joc1);
- joc2.simuleaza();
- System.out.println(joc2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement