Results 1 to 4 of 4
  1. #1
    Nigel is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default relation doesnt work

    I'm trying to create a relation between 2 classes: owner and pet
    I need a methode in the pet class that returns the name of the owner
    I have tried a million things but it just doesn't work plz help me out
    warning the pet and owner both have the field name

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nigel View Post
    I'm trying to create a relation between 2 classes: owner and pet
    I need a methode in the pet class that returns the name of the owner
    I have tried a million things but it just doesn't work plz help me out
    warning the pet and owner both have the field name
    What about this:

    Java Code:
    public class Owner {
       private Pet pet;
       public void setPet(Pet pet) { this.pet= pet; }
       public Pet getPet() { return pet; }
    }
    public class Pet {
       private Owner owner;
       public void setOwner(Owner owner) { this.owner= owner; }
       pubic Owner getOwner() { return owner; }
    }
    After both objects are created you have to set one as a member in the other (and vice versa).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nigel is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    thank you for your replie, I tried what you said and i didnt get any error messages but neither did I get Waht i wanted it's probably my fault so I'll give you my code it's partly dutch i'll put the translation next to it


    public class Eigenaar owner
    {
    private String naamEig; nameOwn
    private String geslacht; sex
    private int leeftijd; age


    and


    public class Huisdier pet
    {
    private String naam ; name
    private String soort ; breed
    private int leeftijd age
    private String eigenaarH; ownerP
    private Eigenaar eigenaar; owner (the line you said to add)



    public Eigenaar getEigenaar()
    { 3x owner
    return eigenaar;
    }

    so when i execute this methode iwant to get the 3 fields of the owner age sex and name

    i already have a setter i think its correct is works when i test is so..

    just in case :

    public void kenEigenaarToe(String nieuweEigenaar) =( set owner (string newOnwer))

    {
    eigenaarH = nieuweEigenaar;
    }

    I would greatly appreciate any help

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    In my terminology (classes) two objects can be linked together as:

    Java Code:
    Pet fifi= new Pet( ... ); // a new Pet
    Owner rambo= new Owner( ... ); // a new Owner
    ...
    
    fifi.setOwner(rambo); //link the two objects
    rambo.setPet(fifi);
    ...
    
    fifi.getOwner(); // returns rambo
    rambo.getPet(); // returns fifi
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. KeyListener doesnt work some times.
    By Addez in forum New To Java
    Replies: 15
    Last Post: 09-21-2010, 04:25 PM
  2. PrintWriter doesnt work :(
    By Addez in forum New To Java
    Replies: 11
    Last Post: 01-17-2010, 05:59 PM
  3. why doesnt my insertion sort method not work?
    By Jeremy8 in forum New To Java
    Replies: 7
    Last Post: 11-15-2009, 02:56 AM
  4. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 09:31 PM
  5. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 04:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •