1

UML diagram

Hello, I'm trying to to convert this UML diagram to java code directly, and below is my code which doesn't seem to compile anyways. I'm not very sure about how to place the optional multiplicities in, e.g. 0..* and 0..1. Thank you for your help.

public class Person{
    private String name;
    private Person mom;
    private Person dad;
    private ArrayList<Person> child;
    private ArrayList<Person> friend;
    private ArrayList<School> alumni;
    private School current = new School();

    public Person(String name, Person mom, Person dad, ArrayList<Person> child, ArrayList<Person> friend, ArrayList<School> alumi, School current){
        name = this.name;
        mom = this.mom;
        dad = this.dad;
        child = this.child;
        friend = this.friend;
        alumni = this.alumni;
        current = this.current;
    }
}

public class School{
    private String name;
    private ArrayList<Person> student;

    public School(String name, ArrayList<Person> student){
        name = this.name;
        student = this.student;
    }
}
SteamboatTMR
  • 65
  • 1
  • 7

2 Answers2

0

There is no standardized way of converting UML to Java, but I can tell you what is correct and not correct based on the UML and Java semantics.

An association between classes C1 and C2 may be implemented by a property in class C1, or by a property in class C2 or by properties in both classes. If an association does not have an arrowhead, all three options are possible and it is not defined which of these three options is the best. If an association is an arrow pointing from C1 to C2, then the first option is the best, the second option is incorrect and the third option is allowed. I have checked your Java code and it complies to these rules.

If class C1 has a property P implementing an association between class C1 and class C2 and the association has a multiplicity of 0..1 at the side of C2, then P should have type C2 and C1 should have a constructor that does not initialize P. Your Java code is incorrect, because you should not initialize Person.School.

If the multiplicity is * or 0..*, then P should be some kind of collection of C2. Class C1 should have a constructor that either does not initialize P or that can initialize P with an empty collection. The latter is the case in your Java code.

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
  • So that means that, if c1 has 0...1 c2, then c1 should keep a P of type C2 and have a constructor that doesn't require P, if it's 0..* , then either constructing without P or with an empty collection. In that case, is there a need to also include a constructor that includes P for the case of 0..1? – SteamboatTMR Sep 25 '19 at 12:44
  • 1
    No, such a constructor is not required. – www.admiraalit.nl Sep 25 '19 at 14:06
0
public class Person{
    private String name;
    private Person mom;
    private Person dad;
    private ArrayList<Person> child;
    private ArrayList<Person> friend;
    private ArrayList<School> alumni;
    private School current = new School();

This part seems correct except for the School which should be null as the person might not be studying at the moment.

private School current;

keyword this (see Using the this Keyword) refers to current object in Java so the constructor should be written as

public Person(String name, Person mom, Person dad, ArrayList<Person> child, ArrayList<Person> friend, ArrayList<School> alumi, School current){
    this.name = name;
    this.mom = mom;
    ...
}

same problem with the School class:

public class School{
    private String name;
    private ArrayList<Person> student;

    public School(String name, ArrayList<Person> student){
        this.name = name;
        this.student = student;
    }
}

if you are defining both classes in the same file you have to remove public from the class definition of School, which becomes then

class School{

or move to a file called School.java see oracle.com/javase/tutorial/java/javaOO/nested.html

J-M
  • 103
  • 5
  • Thank you for pointing about "this", in this case, can I assume that if its not a mandatory 1, then I shall not instantiate it in my property? – SteamboatTMR Sep 25 '19 at 12:48