Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: bidirectional association example, please help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default bidirectional association example, please help

    Hi, being new to java have difficulties with one of my projects to do and hope you can help me fix the code and comprehend it.

    here is the thing I must do:
    write code for a class dog, dog object is to have attributes name, age, address

    write code for a class flea, a flea object is to have attributes name, age

    give any additional code in the flea and dog classes that is reqired to setuo a bidirectional association between flea object and a dog boject.
    a dog object acts as a host for a flea object and the flea acts as a parasite for a dog.
    your code does not have to be robust.

    modify dog class so that a dog object can act as owner for up to 200 flea objects. as part of this code give a methoid which returns all parasites for a particular dog.

    write test program which creates a dog object and several flea objects
    make dog the host for flea and each flea a parasite for a dog. ask the dog "who are your parasites " and point the results
    dont read in info from the user. instead create all objects in code
    eg - employe j = new employe("joe")


    and what I have so far:

    import java.util.Collection;
     
     
    public class Dog {
     
    private String name;
    private String Address;
    private int Age;
     
     
    private Collection<Flea> Fleas;
     
    public void setName(String name)
     
    {
    this.name = name;
    }
     
    public void setAddress(String Address)
     
    {
    this.Address = Address;
    }
     
    public void setAge(int Age) {
     
     
    this.Age = Age;
     
      }
     
    public String getName(){
    return name;
    }
     
     
     
    public String getAddress(){
    return Address;
    }
    public int getAge() {
     
    return Age;
     
      }
     
     
    public String toString(){
    return "Name: "+ name +"\n" +  "Address: "  + Address +"\n"+ "Age: " +Age ;
    }
     
     
     
     
     
      public void addfleass(Flea flea) {
     
     
     
    if (!Fleas.contains(flea)) {
     
     
    Fleas.add(flea);
     
     
     
    }
     
      }
     
     
     
      public Collection<Flea> getFleas() {
     
          return Fleas;
     
      }



     Dog[] Flealist;
    public Dog() {
    Flealist = new Dog[200];}

    import java.util.ArrayList;
    import java.util.List;
     
    public class DemoAnimals {
     
    public static void main(String[] args) {
    String n;
    Dog Ralph = new Dog();
    Ralph.setName("Ralph");
    Ralph.setAddress("Cork");
    Ralph.setAge(12);
    Flea Stuart = new Flea();
    Stuart.setName("Stuart");
    Stuart.setAge(12);
    Stuart.setDepartment(Ralph);
    n= Ralph.toString();
    System.out.println(n);
     
    }
     
    }

    public class Flea {
    private String name;
    private int Age;
    private Dog Ddog;
     
    public String getName(){
    return name;
     
    }
     
    public void setName(String name){
    this.name= name;
     
    }
     
    public int getAge(){
    return Age;
     
    }
     
    public void setAge(int Age){
    this.Age= Age;
     
    }
    public Dog getDog() {
     
     
     
      return Ddog;
     
        }
     
     
     
        public void setDepartment(Dog Ddog) {
     
     
     
      this.Ddog = Ddog;
     
        }
     
     
    public String toString() {
     
    return "Name: " + name + "Age: " + Age;
    }
     
     
    }


    THANKS!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bidirectional association example, please help

    help me fix the code
    Do you have any specific questions about your problem?
    What needs fixing? What does the code do that you want to change?

    The posted code is not properly formatted. Too many statements start in the first column. Nested statements need to be indented 3-4 spaces to make it easier to read and understand the logic.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. association help !!
    By jove in forum Object Oriented Programming
    Replies: 2
    Last Post: August 1st, 2011, 12:33 PM