-3

I have to transform this class diagram into java code:

enter image description here

This is how I implemented it:

public class domandaAperta {
       
    private String rispostaCorretta;

    public opzione[] m = new opzione[2][5];
    
                  ..............

}

public class opzione {

        private String testo;
                 ..............
}

Is my code correct?

Christophe
  • 68,716
  • 7
  • 72
  • 138
saretta2
  • 37
  • 5
  • Obviously you did not implement `opzione` (why the lower case??). Neither did you implement `method` (which is a superb name for an operation). Besides: why 10? Only 5 are required. Also note that SO is not a homework checking place. – qwerty_so Jul 15 '21 at 12:38
  • @qwerty_so I think I melted, I edited the post, 10 because 2*5 – saretta2 Jul 15 '21 at 17:46
  • 2*5=10 sure. But where from do you get the times two? – qwerty_so Jul 15 '21 at 19:27
  • You might want to read up on the UML basics. Multiplicity `[2..5]` means each `domandaAperta` instance will be linked to least 2 and maximum 5 `opzione` instances – Geert Bellekens Jul 16 '21 at 07:35
  • 1
    Also, it's kind of rude to change your question completely after someone took the time to write an answer. By editing your code (based on the answer given) you are completely invalidating the answer. – Geert Bellekens Jul 16 '21 at 07:36

1 Answers1

1

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

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • hello, thanks for the answer but in this exercise I just have to translate the uml diagram in java, I know how it translates but my problem was to represent the association 2 ... 5 in java, so as you said you have to create a multidimensional array like this : Opzione[][] Opzione = new Opzione[2][5]; from domandaAperta – saretta2 Jul 16 '21 at 07:15
  • @saretta2 in java you cannot as fas as I know create hard bounds for arrays. Structurally having the array but initializing it with an optione[2] would meet the minimum requirement. [2][5] would be two-dimensional and wrong here. There are more than one way to implement this. But sine you must have at least 2, it would seem logical that the constructor takes 2 optione arguments and initializes the array accordingly. The upper bound could be imposed only with checks on operations that add items (e.g. addOpzione()) but there is no such operation defined. – Christophe Jul 16 '21 at 08:32
  • @saretta2 still another way would be to have a constructor that takes an array of optione as argument, checks that there are at least 2 and copy at most 5 of them into the local array. – Christophe Jul 16 '21 at 08:34
  • okay in code can you tell me how to do it? – saretta2 Jul 16 '21 at 09:03
  • I would like to do it with the last way you told me – saretta2 Jul 16 '21 at 09:20
  • 2
    @saretta2 I will not do the exercise for you. But here a hint about constructors in Java: https://www.w3schools.com/java/java_constructors.asp and here some hints about passing array arguments to methods (it’s the same for constructors) and accessing their number of items: https://www.tutorialspoint.com/How-to-pass-Arrays-to-Methods-in-Java - And finally, here is how to deal if the length of the argument is invalid: https://stackoverflow.com/q/6086334/3723423 . Hope this helps. Up to you, now. Further java implementation questions not related to UML should be dealt with in a separate question – Christophe Jul 16 '21 at 09:51