Question

I am attempting to convert this diagram in to java code just to get a hang of design questions that can be asked.

For instance the Piecetype enum i would write as

public enum PieceType{
    pawn, king, queen, rook, bishop, knight
}

I was wondering if someone can provide guidance as to how to convert the class diagrams in to the java code just literally take one of the classes shown in the diagram and write the attributes and methods mentioned in java .

I am mostly having hard time figuring out the + and -.

enter image description here

Was it helpful?

Solution

+ and - refer to visibility:

Symbol  Visibility
------  ----------
+       Public 
-       Private 
#       Protected 
/       Derived (can be combined with one of the others)
_       Static

(source)

OTHER TIPS

First you should learn how to read the class diagram properly before trying to implement it in Java. The + and - define the visibility of a class member (e.g. field or method). A + denotes a public visible class member, while a - represents a private member, i.e. only accessible from the class where it's defined.

Example of conversion to a Java class (Game):

public class Game {
   private List<Move> playedMoves;
   private PieceColor turn;
   private Players[] players;
   private Result result;
   private CheckStatus checkStatus;

   public Game() {
     playedMoves = new ArrayList<Move>(); // create empty list
     // TODO initialize other fields properly
   }

   public void addMove(Move move) {
     playedMoves.add(move);
   }

   public boolean isCheckmated() {
     return CheckStatus.BlackCheckmated.equals(checkStatus) || 
            CheckStatus.WhiteCheckmated.equals(checkStatus);
   }

   // TODO add other methods
}

References:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top