Lesson 5

Class diagrams

Objectives

Understanding class diagrams and how to model a java application using class diagrams.

Documentation

Exercises

Exercise 1

Implement in Java class diagram from figure bellow.

Exercise 2

Implement in Java class diagram from figure bellow.

Exercise 3

Implement in Java class diagram from figure bellow.

Exercise 4

Implement in Java class diagram from figure bellow.

Exercise 5

Create the class diagram for the application bellow.

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();
	}
 
}

Exercise 6

Implement in Java class diagram from figure bellow.