• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Using Recursive Classes and Invoking Other Classes

 
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a small app in Eclipse Mars that lists the family tree in a recursive manner.
To do this, I am using a package with 3 class files:
TestName ... gets name of person (hard coded it for now)
TestPerson ... invokes AName() method in TestName, then get gender and ssn ( also hard coded for now)
FamilyTree ... invokes APerson() method, then recursively list names hierarchically in family

Problem:
In the test driver FamilyTree, the class Family does not access its own mutators, instead it goes out to TestPerson to invoke the mutators to set the names.
Eclipse Mars even created the setFather() and setMother() mutator methods in TestPerson
This is NOT my intention. I want FamilyTree to use the setMother() and setFather() methods that I have defined in FamilyTree and not go and create, then invoke those methods in another class.

Code snippet for FamilyTree follows, as you can see I've the mutator methods setFather() and setMother():


And this is how I hard code (got to begin somewhere) and insert names into the mutator objects:



 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking over your code and in the first listing you have a bunch of methods, which you way are Constructors and in the second listing you have the Class, they are all in the file FamilyTree.java.

Your setters, "mutators" are in your class APerson correct?

So when you make your "recursive calls", which from what I can see here they are not recursive, you get an error, but you do not tell what error is generated--that is, to me, one of the key elements to decoding your problem. Please tell us what errors you are getting.



 
Les Morgan
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are essentially trying to make a doubly linked list:

which is doable, but the fragmentation and the lack of error in your file post makes it so I do not see what is causing the error.

BTW: in your post FamilyTree hasn't any setters, "mutators".
 
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think that is the longest between logging in and first post I have ever seen: over 11 years.
I would suggest you don't use a char for sex. Use an enum.
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Les Morgan

Thanks for replying and sharing your insight, I will change ssn type to enum once I got the logic to work.
I found the error, I did not change the class name APerson{...} when I cut-n-pasted that into FamilyTree.
Java allows for methods with the same name in the same class-file as long as the have different parameters, but not same class-name in different class files in the same package.
FYI, I use setters for changing ssn in both TestPerson and FamilyTree, at the moment, I do not know whether that s a wise thing to do:

 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Les Morgan

Yes, it looks fragmented for now, I'll put a lot more logic in there once I got this working
Again, thanks for sharing your insight
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie

Yes, it has been a while since I last coded in Java

Thanks for your tip on using enum for gender rather than String, will change it after I got the rest of the logic working
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Les Morgan

The general idea is for the driver to cycle through the list and on each cycle, create a new class-object ww, cw etc...

I hope this line of thinking will work once I try to access the recursive tables.

I can't think of a better way of doing it, for now...

 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic