Click to See Complete Forum and Search --> : Composition & aggregation


cashmoney
March 9th, 2009, 11:06 AM
Hi, beginner in java I have a littles problems with my classes when I implant by aggregation my class "Enfant" on my Class "Salarie".

Ps: I used php [php] because I would like my codes be viewed

Class SALARIE


public class Salarie {
private static int compteur = 0;
protected int matricule;
protected int categorie;
protected int service;
protected String nom;
protected double salaire;
private Vector listeEnfants = new Vector ();
private Adresse adrSal = new Adresse();

public Salarie(int inMatricule, int inCategorie, int inService,
String inNom, double inSalaire) throws SalaireException,
CategorieException {
compteur++;
if (inSalaire < 0)
throw new SalaireException(inSalaire);
if ((inCategorie != 1)&&(inCategorie!=2)&& (inCategorie != 3))
throw new CategorieException(inCategorie);
else {
this.matricule = inMatricule;
this.categorie = inCategorie;
this.service = inService;
this.nom = inNom;
this.salaire = inSalaire;
adrSal.setNomRue("Don");
adrSal.setCodePostal(59240);
Vector listeEnfants = new Vector ();
listeEnfants.add(new Enfant("www","zzzz","124528",this));
}
public String toString() {
return matricule + ", " + categorie + ", " + service + ", " + nom+", "+adrSal.toString()+ ", " + salaire +", "+ listeEnfants.toString() ;
}
}



Class ENFANT


public class Enfant {
private static int compteur = 0;
private String nom;
private String prenom;
private String dateNaiss;
private Salarie refSalarie;

public Enfant(String inNom, String inPrenom, String inDateNaiss,
Salarie inRefSalarie) {
compteur++;
this.nom = inNom;
this.prenom = inPrenom;
this.dateNaiss = inDateNaiss;
this.refSalarie = inRefSalarie;
}
public String toString()
{
return nom+", "+prenom+", "+dateNaiss+", "+refSalarie;
}
}



In my main Class to test my classes I have



import java.util.*;
public class TestSalarie {
public static void main(String[] argv) throws SalaireException, CategorieException {

Salarie sal1= new Salarie(2, 2, 7, "forum", 35000.00);
System.out.println("TO STRING SALARIE : "+sal1);
}
}



When I compile it, this my result.


TO STRING SALARIE : 2, 2, 7, forum- Don- 59240, 35000.0, []


The report is that I see [] instead of informations about my class "Enfant" bound to our "Salarie". And if you have to notice, my vector which I declared more, it was twice declared there private and then in my builder of initialization of my class Salarie. when I delete my vector in my builder of inistialization, I have errors and lot of exceptions. I give you result there.


Exception in thread "main" java.lang.StackOverflowError
at sun.misc.FloatingDecimal.dtoa(Unknown Source)
at sun.misc.FloatingDecimal.<init>(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Object.Salarie.toString(Salarie.java:132)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Object.Enfant.toString(Enfant.java:34)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.util.AbstractCollection.toString(Unknown Source)
at java.util.Vector.toString(Unknown Source)
at Object.Salarie.toString(Salarie.java:132)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Object.Enfant.toString(Enfant.java:34)
at java.lang.String.valueOf(Unknown Source)


Thanks for your help!!!

ProgramThis
March 9th, 2009, 12:13 PM
Ok, the problem is that your Enfant object has a Salarie object that is prints out as a part of it's toString() method. In the Salarie toString() method you are trying to print out an Enfant object... so around and around you go until you get a stack overflow because you are printing out both objects in each others toString, which continously calls the other objects toString.

cashmoney
March 10th, 2009, 04:25 AM
So, it makes an Infinite recursion ?

I would like put on "refsalarie" of my class "Enfant", the "matricule" from my class "Salarie" but I do not have idea to do that. I am beginner in java.

ProgramThis
March 10th, 2009, 08:52 AM
Yes, it makes for an infinite recursion. You are calling class A which calls class B which calls class A again which... and so on and so on. Maybe you could explain better what exactly it is you are trying to accomplish and why you have references in each class of the other classes. That might help us to help you better.

cashmoney
March 10th, 2009, 10:41 AM
Ok, sorry for my english but I want to reference the "matricule" of my Class "Salarie" to my "refSalarie" to my class "Enfant" to have informations about childs parents who are bound by "refSalarie".
I explain well
" Enfant " in english "Child"
"Salarie" in english " Employee"
" matricule" in english "roll"
So we know that each employee(Salarie) have Children(Enfant), and each Children have a Employee parents Referenced by a number roll(matricule).

In my 'toString', when we display informations about a child, we must to have his employee parent roll number.
Or when we want to display informations about an employee, we must to have information about his child.

I hope, it is clear for you.
Thanks

ProgramThis
March 10th, 2009, 11:25 AM
Oui c'est mieux. (no, my french is not as good as your english :) )


Ok, so what you need is to print out the matricule of the parent (Salarie) in the toString of the Child(Enfant). So, instead of doing:

return nom+", "+prenom+", "+dateNaiss+", "+refSalarie;

In your Enfant class toString method, do this:

return nom+", "+prenom+", "+dateNaiss+", "+refSalarie.getMarticule();

You will of course have to provide a getMarticule() method in your Salarie class to return that value, but this will allow you to have the information you want (the marticule) without having to print out ALL of the data associated with a particular Salarie.

C'est bon?