Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is this implementation of the UML diagram correct?

Is this implementation of the UML diagram correct?

Im preparing for exams, our professor did not give us a solution to these tasks because we are supposed to be active ourselves but I’d still like to have feedback.
enter image description here

My Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public abstract class Employee {
   protected String name;
   protected int salery;
   public Employee(){
      name="";
      salery=0;
   }
   public String toString(){
       return "name: "+name+"salery: "+salery;
   }
}

import java.util.ArrayList;

public class EmployeeList {
    protected ArrayList<Employee> members;

    public EmployeeList(){
         members=new ArrayList<Employee>();
    }

    public String toString(){
        String ret="";
        for(int i=0;i<members.size();i++){
             ret+=members.get(i).toString()+" ";
        }
        return ret;
     }
}
public class Manager extends Employee{
     protected String department;
     public String toString(){
          return department;
     }
}

The Code seems to work in practice because I’ve tested it with:

    EmployeeList employeeList=new EmployeeList();

    Manager m1=new Manager();
    Manager m2=new Manager();
    Manager m3=new Manager();

    m1.department="McDonalds";
    m2.department="BigHouse";
    m3.department="Heart";

    System.out.println(m1);

    employeeList.members.add(m1);
    employeeList.members.add(m2);
    employeeList.members.add(m3);

    System.out.println(employeeList);

>Solution :

You (should) lose some marks for a significant number Java style errors and for misspelling salary as salery. Yes, these things do matter.

It is also debatable whether you should have declared the fields as private and provided protected getters and setters. (It is not normal practice to put getters and setters into UML diagrams. They are kind of assumed. But it is (normally) bad practice in Java to declare non-private fields.

Finally, Employee should not be abstract. (It doesn’t even make sense that you can only create Manager objects and not Employee objects.)


In practice, the Java classes (or whatever) do not need to be literal translations of the UML. UML is a design notation … not a specification language.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading