Search code examples
javauml

Converting a UML Diagram into Code


I am having issues converting this UML Diagram setup into code. I understand the Class is the top portion followed by the variables and the methods. I am very new to Java and this is probably the most confusing thing I've ever encountered. I'm not 100% sure how to create the "model" or the main code section for this but this is the code I have started/attempted for the Domain class so far. Am I on the right track? Or am I just completely lost. Any tips, or something to send me in the right direction would be greatly appreciated.

enter image description here

  //This is the main .java file
  public class Assignment3 extends Application {



@Override
public void start(Stage stage) throws Exception {
    Group root = new Group();
    Scene scene = new Scene(root);
    Canvas canvas = new Canvas(400, 300); // Set canvas Size in Pixels
    stage.setTitle("FXGraphicsTemplate"); // Set window title
    root.getChildren().add(canvas);
    stage.setScene(scene);
    GraphicsContext gc = canvas.getGraphicsContext2D();

    Castle stateDomain = new Castle();
    Castle drawDomain = new Castle();

    stage.show();
    }
 

 public static void main (String[] args) {
     launch ( args );
 }

 // .class file that is utterly awful and I don't know where to go
 
 public class Castle {


 private String castleName;
 private double castleSize;
 private double castleX;
 private double castleY;
 private int domainX;
 private int domainY;
 private String castleColor;
 private String domainName;



public void stateDomain ( String castleName, int domainX, int domainY, String castleColor ) {
    this.castleName = castleName;
    this.domainX = domainX;
    this.domainY = domainY;
    this.castleColor = castleColor;    
}

public void drawDomain ( GraphicsContext gc ) {
    
    
    
}

I want to point out I am just looking for some direction. Anything at this point would be incredibly helpful, whether it's a link to another post or a sliver of code that gets my brain going. Thank you.


Solution

  • The most abstract way to describe static relationship between classes is using the Association link, which simply states that there is some kind of a link or a dependency between two classes or more. There is the weak and the Strong Association. For your case i will consider it to be the strong association.

    public class Domain 
    {
        private double x, y, size;
        private String name;
    
        public void state(String name, int x, int y, Color color) {
                //TODO
        }
    
        public void draw(arguments) {
            //TODO
        }
    }
    
    
    public class Castle
        {
            //all arguments in diagram and now you need a reference to the class Domain
    
            private Domain domain;
    
            //The rest is just writing what is in the diagram
    
        }
    

    but i must say your diagram is very incomplete. Please take a look at: https://en.wikipedia.org/wiki/Class_diagram