From diagram to code

Turning a class diagram into Java code: visibilities, attributes, an association becoming an attribute or a collection depending on the multiplicity, navigability, generalisation, realisation and composition.

  • 21 cards
  • 4 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Understand

A few cards from the deck

  • Translating a visibility: what is it?

    Show the answer

    + becomes public, - becomes private, # becomes protected, and ~ has no keyword: you write nothing.

    Java's default level, with no keyword, is exactly package visibility.

  • In the diagram, Equipe is joined to Joueur by an association navigable from Equipe to Joueur, with 0..* on the Joueur side. Which declaration goes in Equipe?

    • private List<Joueur> joueurs = new ArrayList<>();
    • private Joueur joueur;
    • private List<Equipe> joueurs = new ArrayList<>();
    • private static Joueur joueurs;
    Show the answer

    private List<Joueur> joueurs = new ArrayList<>();

    Zero or more players: the upper bound exceeds 1, so a collection of Joueur, created empty so it is usable. A single reference can only hold one, and static would share the attribute across all teams.

  • An underlined member translates to …; an italic member, to ….

    Show the answer

    An underlined member translates to static; an italic member, to abstract.

    An abstract member forces its class to be abstract too: the class name itself goes italic in the box.

  • Navigability: what is it?

    Show the answer

    The direction in which one object can reach the other, shown by an open arrow at the end of the line; only the starting object keeps a reference.

    Order → Line gives an attribute in Order and none in Line. With no arrow, the direction is left open, or both sides get an attribute.

  • An association is navigable from Commande to Ligne only. Why does only Commande get an attribute?

    • The arrow says which class keeps a reference to the other
    • Java forbids two classes from referencing each other
    • An attribute placed in Ligne would have to be public
    • An attribute in Ligne would cause an endless loop at construction
    Show the answer

    The arrow says which class keeps a reference to the other

    A reference is only worth keeping if the code needs it. Adding an attribute in Ligne would create a useless dependency and two references to keep consistent; two classes can of course reference each other when you decide so.