Vous êtes sur la page 1sur 2

Assignment 4 Classes and Inheritance

Your task in this assignment is to write a program based on the UML class diagram below.
It’s a good idea to start getting familiar with this method of documenting software.

In a UML diagram:

 Each block represents a class (e.g.: Zoo, Cage):


 In a block, the variables are listed first, together with their types:
o placeNr : int represents an integer variable placeNr
o name : String represents a String variable name
o the square brackets after a variable name [] represent a collection (e.g. an array).
The number in the brackets tells how many elements are stored in it:
- an integer number represents a fixed size
- a range of possible sizes is given as e.g.: 1..5 (from 1 to 5 elements)
- unbound size is represented with the asterisk: *
o assistants : Assistant[2] represents an array of Assistant variable
of size 2 named assistants
o animals : Animals[*] represents an array of Animals variable named
animals. The size of this array is unbound: potentially it should be able to store
a very big number of elements: it’s a perfect place to use ArrayList<Animal>
instead of plain array here.
 After the horizontal separator, the methods implemented by a class are listed:
o The general form in which methods are represented is:
methodName(param1: type1, param2: type2) : return_type
o Usually the constructor is listed first e.g. Cage(place : int) : void
The return type of the constructor in the diagram is void but in your code a
constructor doesn’t return anything, it is simply public Cage(int place).
o Other methods are straightforward, for example a method of Cage:
+getAnimal(name : String) : Animal
takes one String parameter name, and returns an Animal with this name if it’s
in the Cage or null otherwise.
 The arrows in a UML diagram represent relationships between classes:
 ⇾
A hollow triangle arrowhead ( ) represents inheritance: the arrow points from the
child class to the parent class, for example the arrow between AnimalKeeper and
Employee means that the AnimalKeeper class extends the Employee class.
 An open arrowhead (→) represents association – it is used when one class contains a
variable of some other class type. There are also numbers next to the association arrows
that represent the multiplicity of the association. In the class diagram above there are
four associations:
o Zoo [1→*] Employee: A Zoo object has at least one (and potentially
unlimited) Employees. (They should be stored in a ArrayList<Employee>
class variable of Zoo)
o AnimalKeeper [1→0..2] Assistant: each AnimalKeeper can have from 0
to 2 Assistants (stored in the array assistants)
o AnimalKeeper [1→1] Cage: each AnimalKeeper has one variable of type
Cage
o Cage [1→*] Animal: each Cage can store multiple Animals (in the animals
variable of type ArrayList<Animal>)
 A variable or method with a ‘–‘ before its name is private. So in the class Animal the
variables name, type and birthYear are private.
 A variable or method with a ‘+’ before its name is public. So the methods Animal
(constructor), getName and toString are public.
Extra information & tips:
1. There should be no Employee objects with the same name in the Zoo class.
2. There should be no Animal objects with the same name in one Cage (check for it in the
putAnimal method).
3. The methods are named in such a way that you should be possible to guess what their
functionalities are. For example in the Cage class:
a. putAnimal adds an Animal to a Cage
b. getAnimal returns an Animal with a specific name or null if such an Animal is
not in the Cage
c. animalInList returns true if an Animal with a provided name is in the Cage
or false otherwise
d. remove: removes aa Animal with a given name from the Cage if it exists

Vous aimerez peut-être aussi