Skip to content
Advertisement

How to write constructor where the types are from another class in Java?

I’m doing a UML diagram for a Connect four game. It is a two-player connection board game, in which the players choose a color and then take turns dropping colored discs into a seven-column, six-row vertically suspended grid.

I have a problem with the UML Diagram. The class game has 2 variables that came from another class(is this ok?). And secondly how is possible to write the constructor for all the given variables?

Notice:The purpose of UML Diagram is not to write code out of it, but only as a project. There are no requirements.

UML Connect four

Advertisement

Answer

Classes with properties of other types

Let’s take the example of Game and Player. In your diagram, you show a property playernames: Player[]. This means that the class Game is implicitly associated with the class Player. At the same time you show an explicit association with the Player class. This is ambiguous: are both implicit and explicit associations the same ? Or are these two different associations?

A less ambiguous representation is to remove the property from the compartment of the class, and show its visibility and name as a role on the association line at the opposite end.

Another example is the class’ Game property board:Board. At the same time, there is an association to a Gameboard. Are these two the same, and there is a mistake in one of the class name? Or did you forget a «interface» Board in your diagram, to which Game should be associated and that Gameboard would implement ? Up to you to clarify.

If possible, indicate the multiplicity of the associations. If it’s exactly 1, or exactly 2, write it down. If it’s several write a *.

Constructors

In UML, a constructor is an operator like any other. It could have any name and should return a type corresponding to the owning class. It should be preceded by the stereotype «Create»:

+ «Create» Game():Game 

In your class you also have a related operations (remark: in UML an operation that does not return anything does not return void, but nothing at all):

+ initialize() 
+ setBoard (board: Board)

The reader can infer that the board is created elsewhere and probably needs to be set. It is however not specified in your diagram which of the constructor or the initializer creates/constructs the other objects for the remaining propertiers. If this matters for you, you could add a comment note in plaintext. If you’d prefer a more formal way, you could express a post-condition instead.

If Game does not create some of its properties, for example, if your user interface would create Player objects that would then be passed to the constructor, you’d indicate these objects in your operation specifications, in the same way than in Java:

+ «Create» Game(player: Player[2]):Game 

But if you set the board, you could also set (or add) the players.

Unrelated remarks

  • Several Game operations return Person but there is no class like this. (You could add such a class in your diagram and draw a dependency).
  • The name of the property playernames is misleading: it’s players, and players are not just the names. It’s also the points, the state, annd everything that makes a player.
  • can you explain why you’ve put arrows on the associations ? If not, it’s probably better to remove them.
  • Extends is not an UML keyword. Just remove it: the specialization that you have drawn is sufficient: it means inheritance. For intertaces that are implemented you’d use the same symbol but with a dashed line.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement