Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set up bidirectional association between two objects in OO Java
    text
    copied!<p>I have a basic assignment to do but am very new to OOP and struggling with it. Other online resources are starting to add to my confusion.</p> <p>I am required to:</p> <ol> <li><p>Write code for a class Person. A Person object is to have attributes name, age and address.</p></li> <li><p>Write code for a class Dog. A Dog object is to have attributes name and age. </p></li> <li><p>Give any additional code in the Person and Dog classes that is required to setup a bidirectional association between a Person object and a Dog object. A Person object acts as an owner for a Dog object and the Dog object acts as a pet for the Person object. </p></li> <li><p>Modify your Person class so that a Person object can act as owner for up to 20 Dog objects.</p></li> </ol> <p>Obviously this is a very simple example.</p> <p>My <strong>code</strong> so far: </p> <p><em>Person Class :</em></p> <pre><code> public class Person { // instance variables - replace the example below with your own private String name; private int age; private String address; /** * Constructor for objects of class Person */ public Person() { this.name = name; this.age = age; this.address = address; } //Set Methods: public void setName () { this.name = name; } public void setAge () { this.age = age; } public void setAddress () { this.address = address; } //Get Methods: public String getName () { return name; } public int getAge () { return age; } public String getAddress () { return address; } } </code></pre> <p><em>Dog Class:</em> </p> <pre><code> public class Dog { // instance variables - replace the example below with your own private String name; private int age; public Dog() { this.name = name; this.age = age; } //Set Methods: public void setName () { this.name = name; } public void setAge () { this.age = age; } //Get Methods: public String getName () { return name; } public int getAge () { return age; } } </code></pre> <p><em>Main:</em> </p> <pre><code>public class Main { //Blank } </code></pre> <p>I know this code is currently useless and doesn't do anything but I am unsure of how to 'associate' the objects &amp; where to do it. The assignment spec specifies a person acts as an 'owner' for the dog. </p> <p>This is where my problem lies. Setting up the relationship between the objects. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload