Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UML to java code

Tags:

java

uml

I trying to understand UML class diagrams, but i am not sure if I do ... Lets assume that we have an UML class diagram like this (only required attributes are shown in each class): UML class diagram

The java code for that UML class diagram above should look like this (if I'm not wrong)

class Car {
    private String color;
    private int weight;
    private Gearbox gearbox;
    private Brand brand_name;
}

class Brand {
    private String Skoda;
    private String BMW;
    private Location location;
}

class Location {
    private String US;
    private String EU;
}

class Gearbox {
    enmu Gearbox {
    automatic, manual
    }
}

The question is : Am I correct ? Do I udnerstand it well ?

like image 659
Julian Brantner Avatar asked Jan 19 '26 18:01

Julian Brantner


1 Answers

Here:

class Gearbox {
    enum Gearbox {
    automatic, manual;
    }
}

That better be:

enum Gearbox { AUTOMATIC, MANUAL; }

There is "no" point in wrapping an enum into a class of the same name in Java.

And from a "modeling" perspective, I would use enums also for location and brand. There is again "no" point in having a brand "class" that has fields called BMW or Skoda. A car has exactly "one" brand out of a known disjunkt set of possibilities and enums exist to express exactly such situations.

Your implementation (except that enum thing) matches the diagram. Yes, But for me correctness also implies that the "big picture" works out. And as explained: your input is inherently flawed. You managed to "correctly" express that input with Java source code. So you solved the assignment (imho). But in the real world I would sent you back home and have you rework the diagram first.

Coming from there, the real answer is: review the UML diagram first. It doesn't make sense this way. So instead of blindly implementing what is given to you - challenge requirements that are obviously inconsistent.

like image 138
GhostCat Avatar answered Jan 22 '26 12:01

GhostCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!