my goal is to convert this class diagram into Java code.
How should I approach this, given that I want the constraints to hold at all times? It creates a chicken-egg problem where the first Course or Student created will always be alone in the world, thus violating the minimal multiplicity constraint of either "teaches" or "takes".
Any suggestions?

enter image description here

  • 2
    The constraint itself violates reality - students not taking any courses and courses without students can happen (at least temporarily). When in doubt, reality wins every time. Feb 18 '19 at 13:44
  • You can disallow creation of Course and Student except from a factory that controls the cardinalities. Of course, that means the factory (and its consuming clients) won't be able to create a Course without both the Course Info and Student or Student info for at least one student, and also, won't be able to create a Student without at least one Course or Course info. This will be extremely awkward to use.
    – Erik Eidt
    Feb 18 '19 at 15:39
  • @ErikEidt yes I think that is a reasonable solution, although awkward I admit.
    – erap129
    Feb 18 '19 at 18:36
  • Yes, I don't think you want to enforce these constraints as absolute within the model, but rather highlight violations on a dashboard or something.
    – Erik Eidt
    Feb 18 '19 at 20:30

I'm of the opinion that we need to model our systems somewhat with the possibilities in mind that business rules & constraints can and will change.

Thus, some constraints are hard model constraints — the system simply won't work properly without them — but others are soft business constraints, which we should consider either dynamically configurable or case-by-case overridable.

I consider the use of constraints other than 0, 1, and * (e.g. 2, 3, 30) as an indicator as to whether constraint is a hard constraint that needs to be enforced within the model for correctness or a soft constraint that can be dynamically configured and/or application-overridden.

Even if the business constraint comes from legal requirements (e.g. in child care there must be at least 1 adult in the room for every 10 children under 4), the laws can change, over time for one, and can vary by district as well.

We can find similar issues (business rules as configuration or soft constraints vs. hard model constraints/requirements) even with the more basic cardinalities.

It might seem reasonable, and be a documented business rule, that a professor has one and only one office.  So, this suggests setting up the model with 1:1 cardinality with these entities.  Then we run into a situation where we have an exceptional professor who is a member of two different departments and she has two offices.

This exceptional situation could be handled in stride in a model with 1:N relationship, while a model with 1:1 would break requiring ugly workarounds — like duplicating the identity and account (userid/password) for the professor.


First of all your class diagram doesn't look quite right. You can't make 2 associations with opposing cardinalities between 2 classes. At least I've never seen one.

What you want to achieve based on the lower association, a course teaches 0...30 students and a students has 1...* courses.

The Student class has a list of its courses and the course class has a list of its students. The constructor of students has a parameter for a course, so that you can only create a students attending a course.

  • You can have to directional associations with different constraints. The problem here is that drawing is either confused or self-contradictory. The constraints, I believe,are that a course can be associated with 0-30 students and that each student can take up to 3 courses.
    – JimmyJames
    Feb 18 '19 at 16:37
  • @Mintri your solution (from what I understand) ignores the fact that a course must have a teacher, therefore a course can't exist on its own.
    – erap129
    Feb 18 '19 at 18:36
  • @erap129 Your diagram suggests no such thing. In the world you've given us, there exists only Courses and Students. "Teacher" is unknown to us. Is it another record somewhere? Is it a string of the teacher's name? Is this a 1:1 mapping of Teachers to Courses, can a Teacher have multiple Courses, can a Course have multiple Teachers, is there an idea of an "Independent Study" where there's no Teacher (or no Course)?
    – Delioth
    Feb 19 '19 at 17:43
  • @erap129 or do students teaches others students in a course?
    – Mintri
    Feb 20 '19 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.