Is your code correct: does it compile?
Your code does not compile:
public type method(type) // ouch!
It is not valid, since Java would understand type
to be a type (e.g. String
, int
, a classname, ...) and would expect an identifier to follow.
To your defence, the UML diagram is not valid in this regar either, since the argument should be in the form argumentName : argumentType
. If one would tolerate a deviation to the UML specification, it would still not be clear what type
represents: name or type (even if we could guess, the latter).
After a correction, the code still would not compile because of two other errors:
- Java is case sensitive and
domandaAperta
is unknown, while DomandaAperta
is well defined.
- the two methods must return a
type
, even if it's not a useful result.
To your defence, the UML diagram uses a lowercase naming for the two classes, whereas the tradition is to use upper case class names in Java. You're not obliged, but whatever your choice, stay consistent.
Is your code correct: does it correspond to the UML design?
The navigable association would indeed usually be implemented with some kind of collection of optione
in domandaAperta
, since the upper bound of the multiplicity is more than 1.
The visibility is not indicated in the diagram: you opted for public, which is not wrong. However, I'd recommend it to keep it private by default.
A more questionnable issue is your initialization of m
with an array of 5 elements (i.e. new opzione[5]
). Ok, you can then assign up to 5 different opzione
. But nothing prevents the code to add more items. Moreover, it would be difficult to know how many itms are really assigned and how many are unassigned.
A safer approach would be to create an array of 2 items, since there must be at least 2 and assign those items. Obviously, you'd be better off with m being private, and having a method for adding or removing items, and which ensure that the mutliplicity stays within expected bounds.
An other issue is the way back from opzione
to domandaAperta
. You have implemented it, but the diagram stays silent about it: the navigability is explicit only in one direction.
Caution: the multiplicity of domandaAperta
is unspecified. There is no reason that you foresee an array with 2 elements. Now your code suggests that perhabs you misunderstood the multiplicity 2..5
as 2 on one side and 5 on the other ? This is incorrect: 2..5 means between 2 and 5 elements.
is your code correct: does it produce meaningful and accurate results?
We can't know, since we have no clue about what it is supposed to do and how you're going to implement it ;-)