Laborator 8

Lab 8

Problema 1

class L1 extends Exception {

public String toString() {

return "L1";

}

}

class L2 extends Exception {

public String toString() {

return "L2";

}

}

public class Main {

public static void main(String[] args) {

try {

int i;

for(i = 0; i < 4; i++) {

if(i == 0) throw new L1();

else throw new L2();

}

} catch(L1 e) {

System.out.println(e);

} catch(L2 e) {

System.out.println(e);

}

}

}

Programul afiseaza L1. Cand i=0, este "aruncata" expceptia "L1" si se iese din ciclul for.

Problema 2

class E1 extends RuntimeException{

}

class E2 extends RuntimeException{

public E2(){

System.out.println("Nu l-am prins");

}

}

class Exemplu {

public void doSomething(int i) {

try {

if(i == 0) throw new E1();

else throw new E2();

} catch(E1 e) {

System.out.println("Prins");

}

}

}

public class Main {

public static void main(String[] args) {

Exemplu e1=new Exemplu();

e1.doSomething(2);

}

}

Problema 3

class E1 extends RuntimeException{

public E1(){

System.out.println("Prins");

}

}

class E2 extends RuntimeException{

public E2(){

System.out.println("Nu l-am prins");

}

}

class Exemplu {

public void doSomething(int i) {

if(i == 0) throw new E1();

else throw new E2();

}

}

public class Main {

public static void main(String[] args) {

Exemplu e1=new Exemplu();

e1.doSomething(0);

}

}

Problema 4

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(String mesaj){

super(mesaj);

}

}

class Gol extends Exception{

public Gol(String mesaj){

super(mesaj);

}

}

class Corner extends Exception{

public Corner(String mesaj){

super(mesaj);

}

}

class Minge {

private int X,Y;

private static final CoordinateGenerator cg=new CoordinateGenerator();

public Minge(int X, int Y){

this.X=X;

this.Y=Y;

}

public int getX(){

return this.X;

}

public int getY(){

return this.Y;

}

public void suteaza() throws Out,Gol,Corner{

this.X=cg.generateX();

this.Y=cg.generateY();

if(this.Y==0 || this.Y==50)

throw new Out("Out!\n");

else

if( (this.X==0 || this.X==100) && (this.Y>=20 && this.Y<=30) )

throw new Gol("Gol!\n");

else

if( (this.X==0 || this.X==100) && ( (this.Y>0 && this.Y<20) || (this.Y>30 && this.Y<50) ) )

throw new Corner("Corner!\n");

}

}

class Joc{

private String echipa1, echipa2;

private int nrGoluriEchipa1=0,nrGoluriEchipa2=0,nrOut=0,nrCorner=0;

public Joc(String echipa1, String echipa2){

this.echipa1=echipa1;

this.echipa2=echipa2;

}

public String toString(){

String result="";

result=result+this.echipa1+" "+this.nrGoluriEchipa1 + " - " + this.nrGoluriEchipa2 + " " + this.echipa2+"\n";

result=result+"Nr out: "+ this.nrOut + "\n"+"Nr corner: "+ this.nrCorner;

return result;

}

public String getEchipa1(){

return this.echipa1;

}

public String getEchipa2(){

return this.echipa2;

}

public void simuleaza() {

Minge minge = new Minge(50, 25);

for (int i = 0; i < 1000; i++) {

try {

minge.suteaza();

System.out.println(this.echipa1 + " - " + this.echipa2 + " Mingea se afla la coordonatele (" + minge.getX() + ", " +

minge.getY() + ")");

} catch (Out out) {

nrOut++;

minge=new Minge(minge.getX(), minge.getY());

} catch (Gol gol) {

if (minge.getX() == 0) {

nrGoluriEchipa2++;

} else {

nrGoluriEchipa1++;

}

minge = new Minge(50, 25);

} catch (Corner corner) {

if (minge.getY()<20){

minge=new Minge(minge.getX(),0);

}

else{

minge=new Minge(minge.getX(),50);

}

nrCorner++;

}

}

}

}

public class Main {

public static void main(String[] args) {

Joc joc1 = new Joc("Steaua","Dinamo");

Joc joc2 = new Joc("Barcelona","Real Madrid");

joc1.simuleaza();

joc2.simuleaza();

System.out.println(joc1);

System.out.println(joc2);

}

}