aspose file tools*
The moose likes Java in General and the fly likes How to make an association class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make an association class?" Watch "How to make an association class?" New topic
Author

How to make an association class?

D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12
Assign is an association class linking instances of Employee with instances of Project. It has no link attributes other than those required to maintain an instance of the association. It should provide accessors for its attributes and an equals() method.

This is the question.

Can someone help me make this or at least help me understand it. I have been searching youtube and books on how to make an association class(at least an example) and all I get is UML diagrams.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Start with this, the heart of what the class is for: "linking instances of Employee with instances of Project." What does that tell you? If you have an "Assign" object, it will convey a relationship between an Employee and a Project. So what do you think you might need, at the very least?

Once you understand that part move on to another small piece. For instance: "provide accessors for its attributes ". Do you know what that means? If not, you need to go back and review that part of your course study.
D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12
The first part im thinking that for every employee made there will be an object made for project and they would both be as one. Like if you call the employee object, that particular employee object will have a project object.

Accessors are methods like setEmployeeName(), etc.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

D Tsafaf wrote:The first part im thinking that for every employee made there will be an object made for project and they would both be as one. Like if you call the employee object, that particular employee object will have a project object.


No, it's precisely because Employee and Project don't directly know anything about each other that we're creating this association class. It's a separate class that says "this employee goes with that project".

Accessors are methods like setEmployeeName(), etc.


Yes, that's the general form, but you won't be setting the Employee name in your association class.
D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12
Ok with that said, how do I make the separate objects know each other?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

D Tsafaf wrote:Ok with that said, how do I make the separate objects know each other?


You don't. You make a new class--an association class--that knows about the two objects. That's the whole point of this exercise.
D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12
Ok I see what you are saying. Can you give me an example of how I am going to link the objects?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

D Tsafaf wrote:Ok I see what you are saying. Can you give me an example of how I am going to link the objects?


That's the job of the association class. By simply knowing about the two objects, the association class links them.
D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12


This is what I came up with
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Not bad . Consider whether to make any of the fields, or the class itself, final. Consider whether you should associate several employees (possibly in a collection) with one project. I think a toString() method would be beneficial. Consider whether you need to override equals and hashCode.
D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12
Campbell Ritchie wrote:Not bad . Consider whether to make any of the fields, or the class itself, final. Consider whether you should associate several employees (possibly in a collection) with one project. I think a toString() method would be beneficial. Consider whether you need to override equals and hashCode.


Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
You’re welcome The formatting could be improved a bit, there is one ( missing and maybe Assign is not the best name for the class.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:Assign is not the best name for the class.


I agree, but I got the impression the instructions required naming it that.

@OP: If you're given the choice, a better name would be Assignment. Depending on the context, we might even skip the abstraction and name it more literally as EmployeeProject or EmployeeProjectAssociation or something like that, if we're trying to follow a convention of mapping class names directly to the DB table they represent.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Jeff Verdegan wrote: . . .
I agree, but I got the impression the instructions required naming it that.
. . .
If that is what the instructions say, that shall be what the instructions get!
D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12
I am having another issue.

getProjects(int ID) - Method Name
Collection of Project objects - Return type
Finds the projects which the employee with the given ID has been assigned to; returns the result in an ArrayList collection. - Description



This is what I have so far. I have found the index of the employee but there is nothing that connects employee to project. I am thinking of using the Assign but I dont see how that will help. Any Advice?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

D Tsafaf wrote:
This is what I have so far. I have found the index of the employee but there is nothing that connects employee to project. I am thinking of using the Assign but I dont see how that will help. Any Advice?


You need to stop coding and think about the problem in th abstract.

First, yes you DO have something that connects Employee to Project. That was the whole point of the exercise so far.

Given what you have so far, and what you know about how that connects Employee to Project, think about how you would find all the Projects to which a given Employee has been assigned. Forget about Java. Just think about it in the abstract, as if you were doing it with pen and paper, or describing the process to someone who was going to do exactly what you tell them to do, but who needs to be told everything precisely and clearly.
D Tsafaf
Greenhorn

Joined: Apr 04, 2013
Posts: 12
I am thinking of I have the employee index in the list. I can then get the employee. The probably put the employee in the Assign to use a get to retrieve the Project
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

D Tsafaf wrote:I am thinking of I have the employee index in the list.


What list?

I can then get the employee. The probably put the employee in the Assign to use a get to retrieve the Project


Which Assign? Either I'm not understanding what you're describing, or you're kind of missing the point. So let's back up a bit.

We've got this Assign class, right? And it tells us that Employee X is assigned to project Y right? So far so good?

So now, although you haven't mentioned this so far, we can probably assume that any one project could have multiple employees assigned to id, and that any one employee could be assigned to multiple projects, right? So that means that somewhere there are going to be a bunch of these Assign objects. I would have expected that your instructor would have mentioned that somewhere, but even if he didn't, for any of this to make any sense at all, it must be the case.

Putting it all together, we have the following:
  • A bunch of employees, kept track of somehow, somewhere, and we don't care about the details of how and where at this point.
  • A bunch of projects kept track of somehow, somewhere, and we don't care about the details of how and where at this point.
  • A bunch of associations between one employee and one project, kept track of somehow, somewhere. All these associations together tell us which employees are assigned to which projects. We don't care about the details of how and where at this point. We don't even care that these associations are represented by the Assign class. We only care that they exist in some logical sense that we can talk about.


  • Right now, it's only the 3rd bullet point we care about.

    So your problem right now is this: We have a bunch of "things", each of which associates one employee with one project. We want to know all the projects that employee X is assigned to. Describe for me, in precise detail, using simple steps, and not mentioning anything Java related, exactly how you would do that.
    D Tsafaf
    Greenhorn

    Joined: Apr 04, 2013
    Posts: 12
    Jeff Verdegan wrote:
    D Tsafaf wrote:I am thinking of I have the employee index in the list.


    What list?

    I can then get the employee. The probably put the employee in the Assign to use a get to retrieve the Project


    Which Assign? Either I'm not understanding what you're describing, or you're kind of missing the point. So let's back up a bit.

    We've got this Assign class, right? And it tells us that Employee X is assigned to project Y right? So far so good?

    So now, although you haven't mentioned this so far, we can probably assume that any one project could have multiple employees assigned to id, and that any one employee could be assigned to multiple projects, right? So that means that somewhere there are going to be a bunch of these Assign objects. I would have expected that your instructor would have mentioned that somewhere, but even if he didn't, for any of this to make any sense at all, it must be the case.

    Putting it all together, we have the following:
  • A bunch of employees, kept track of somehow, somewhere, and we don't care about the details of how and where at this point.
  • A bunch of projects kept track of somehow, somewhere, and we don't care about the details of how and where at this point.
  • A bunch of associations between one employee and one project, kept track of somehow, somewhere. All these associations together tell us which employees are assigned to which projects. We don't care about the details of how and where at this point. We don't even care that these associations are represented by the Assign class. We only care that they exist in some logical sense that we can talk about.


  • Right now, it's only the 3rd bullet point we care about.

    So your problem right now is this: We have a bunch of "things", each of which associates one employee with one project. We want to know all the projects that employee X is assigned to. Describe for me, in precise detail, using simple steps, and not mentioning anything Java related, exactly how you would do that.


    I would say :
    I have employee X. I would find the employee X in the bunch of "things". This should then lead me to where the projects are.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    D Tsafaf wrote:
    Jeff Verdegan wrote:
    I would say :
    I have employee X. I would find the employee X in the bunch of "things". This should then lead me to where the projects are.


    Okay, you're getting closer, but still not enough detail.


    Remember, you have an employee ID, and you have a bunch "things" that associate one employee with one project. Let's say they're index cards with an employee's info on one side and a project's info on the other. An employee might be mentioned on multiple cards, and so might a project, because one employee can be involved in many projects, and one project can have many employees.

    I hand you this pile of cards. I say, "Tell me how many projects John Smith is working on". Exactly what do you do, in order to be able to answer that question?
    D Tsafaf
    Greenhorn

    Joined: Apr 04, 2013
    Posts: 12
    Currently, my question states that an employee can only be assigned to 3 projects but I would like to continue this conversation without this in mind till probably I understand.

    To answer your question, I would have to go through each card looking for the employee John Smith. Probably put those cards aside and continue looking.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    D Tsafaf wrote:Currently, my question states that an employee can only be assigned to 3 projects but I would like to continue this conversation without this in mind till probably I understand.


    That's fine, and it's not really relevant. Whether his max is 3 or 300 or 3,000,000, you can use the exact same logic.

    To answer your question, I would have to go through each card looking for the employee John Smith. Probably put those cards aside and continue looking.

    Right. Not necessarily put them aside though, since we don't need a list of the projects, just a count.

    Normally I wouldn't hand you this next piece, but would try to nudge you toward working it out yourself, but at this point I can see you are willing to try to work through the problem yourself, and I think a concrete example will be useful, so here's how I might describe it.

    I would get one of those clicker counter things they use for counting people going through a line to see a show or whatever.
    Set the counter to zero.
    For each card in the pile..
    ..Look at the employee on the card
    ..If the employee ID matches the ID we're asked to count projects for, then....
    ....click the counter (to bump up the count of that employee's projects)
    After we've gone through all the cards, the counter will show how many projects that employee is assigned to


    Do you see what I mean about describing the steps precisely, and without referring to Java or any of our code?

    Once we do that, it becomes much easier to translate those steps to Java than if we just try to go straight from requirement to Java code.
    D Tsafaf
    Greenhorn

    Joined: Apr 04, 2013
    Posts: 12
    OK i see what you are saying. To have a counter to count every time the persons name is found which of course tells us in the end the total number. Cool. I can't remember if I told you but my problem was a bit different but this what you told me gave me an idea.

    Thanks for opening my understanding. Will try something and get back to you
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    D Tsafaf wrote:I can't remember if I told you but my problem was a bit different but this what you told me gave me an idea.


    Oops, sorry if I misunderstood what you're trying to do. I also made an assumption about the "bunch of things" (list of Assigns) being present. But if it helps you understand how to approach this kind of problem, then all's well that ends well.

    Good luck! And post again if you get stuck.
    D Tsafaf
    Greenhorn

    Joined: Apr 04, 2013
    Posts: 12
    Ok this is what I can come up with.



    Question is the method "Finds the projects which the employee with the given ID has been assigned to; returns the result in an ArrayList collection".

    I know this code might seem crappy (structure wise) but can you tell me if i am going in the right direction
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to make an association class?