Laborator 6
Diagrame de clase.
Obiective
Aprofundarea diagramelor de clase.
Aspecte teoretice
Exercitii
1. Implementati in Java diagrama de clase de mai jos.
2. Implementati in Java diagrama de clase de mai jos.
3. Implementati in Java diagrama de clase de mai jos.
4. Implementati in Java diagrama de clase de mai jos.
5. Realizati diagrama de clase pentru a reprezenta relatia dintre urmatoarele clase:
public class Flower { String name; int health; int petals; Flower(){ name = "Narcisa"; petals = 3; } Flower(String n){ name = n; petals = 3; } Flower(String n, int h, int p){ name = n; health = h; petals = p; } void watering(int c){ if(c>0 && c<10) health = health + 1; else if(c>=10 && c<30) health = health + 3; else health = health - 1; if(petals>0 && (c<0 || c>100)) petals = petals - 1; } void displayFlower(){ if(health>0 && health < 30) System.out.println("Flower:"+name+" health="+health +" petals="+petals+" is in good condition"); else if(health>=30 && health < 100) System.out.println("Flower:"+name+" health="+health +" petals="+petals+" is in very good condition"); else System.out.println("Flower:"+name+" health="+health +" petals="+petals+" is bad condition"); } } public class Gardener { String name; int x; Gardener(String n, int i){ name = n; x = i; } void wateringFlower(Flower f){ System.out.println("Gardener "+name+" is watering flower!"); f.watering(x); f.displayFlower(); } } public class Garden { Gardener g; Flower[] flowers; Garden(Gardener a, int nr){ g = a; flowers = new Flower[nr]; for (int i = 0; i < flowers.length; i++) { Flower f = new Flower("lalea", 10, 3); flowers[i] = f; } } void maintain(){ for (int i = 0; i < flowers.length; i++) { g.wateringFlower(flowers[i]); } } } public class Test { public static void main(String[] args) { Gardener g = new Gardener("adi", 25); Garden b = new Garden(g, 10); b.maintain(); } }