consider this example: 1 public class Man { 2 private Woman wife; 3 4 public void setSpoose(Woman newWife) { 5 wife = newWife; 6 newSpoose.setWife(this); 7 } 8 } 9 public class Woman { 10 private Man husband; 11 12 public void setSpoose(Man newHusband) { 13 husband = newHusband; 14 newSpoose.setSpoose(this); 15 } 16} my problem consist in that I want the setSpoose-method in the class Man to call the setSpoose-method in the woman class, wich call the method in the man class and so on. How to avoid this?? regards Christian
Chris IT-C
Greenhorn
Joined: Jun 03, 2001
Posts: 3
posted
0
line 14 ofcause reads newHusband.setSpoose(this); Christian
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Chris, Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform [This message has been edited by shailesh sonavadekar (edited June 24, 2001).]
See? name changed...so if anyone could advice on my problem...,please Regards Christian
Christian Staermose
Greenhorn
Joined: Jun 24, 2001
Posts: 3
posted
0
To recap: 1 public class Man { 2 private Woman wife; 3 4 public void setSpoose(Woman newWife) { 5 wife = newWife; 6 newWife.setSpoose(this); 7 } 8 } 9 public class Woman { 10 private Man husband; 11 12 public void setSpoose(Man newHusband) { 13 husband = newHusband; 14 newHusband.setSpoose(this); 15 } 16} my problem consist in that I want the setSpoose-method in the class Man to call the setSpoose-method in the woman class, wich call the method in the man class and so on. How to avoid this?? regards Christian
Carl Trusiak
Sheriff
Joined: Jun 13, 2000
Posts: 3340
posted
0
In any situation, you need to set a stop reason to avoid infinate recursion. Your example lends itself to the rule (in most societies) that one man can only have one wife and one women can only have one husband. adding a test to ensure husband or wife is not null sould satisfy this rule.
Since husband sets the wife (or wife sets the husband first, you can allow the set if the other already declared you as their spouse. In other societies, you can create a wife[] and/or a husband[] and iterate through it to see if that man or women already exists in the array and exit if it does.
------------------ Hope This Helps Carl Trusiak, SCJP2 [This message has been edited by Carl Trusiak (edited June 24, 2001).]
Thanks Carl. That solves it. I will be adding a divorse function, so people can change spoose Thanks Christian
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
I know this won't really affect the execution of your program at all, but I feel compelled to point out that the word you're looking for is "spouse", not "spoose".
"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.