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:
Code :
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;
}
Code :
Dog[] Flealist;
public Dog() {
Flealist = new Dog[200];}
Code :
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);
}
}
Code :
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!
Re: bidirectional association example, please help
Quote:
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.