# Java Recursive method problem

Printable View

• 02-25-2009, 06:41 AM
kj2009
Java Recursive method problem
Hi,

Can someone help me or give me some pointers to complete the test() method here. It needs java recursion which i'm not much familiar with.
(This is not a college assignment. It's just that I'm a JSP, Struts person..and have little knowledge of recursion. )

Problem statement...

You go to a wedding reception, and there are a lot of people. All of these
people 'know' the bride or the groom in some manner.

Ann is the bride's sister.
David is the groom's boss at work.
Ann is going out with David's brother Cain.

So - if you look at the relationships between these two, you have:

Ann goes out with Cain. Cain is a brother of David.
Ann is a sister of Bride. Bride is married to Groom. Groom works for David.

So here's the problem...

Assume a Table (the table will be mocked up in memory)...

The Table has entries for each person's relationships to people at the wedding.

The problem is to write a RECURSIVE method that will take the names
of two people as input. It then needs to output all the ways in which
these people are related much in the same fashion as we showed in the list of
relationships between Ann and David above.

*/

package com.oneshield.test;

import java.util.ArrayList;

public class WeddingTester
{
ArrayList assocations = new ArrayList();

public static void main(String[] args)
{
WeddingTester tester = new WeddingTester();

tester.test();
}

/**
* Implement your solution here...
* Add methods and variables as needed...
*/
public void test()
{
try
{

}
catch(Exception ex)
{
ex.printStackTrace();
}
}

/**
* This method is used to search for a persons associations
*
*
* @param person - The name of the person to find associations for...
* @return - An ArrayList of Associations
*/
public ArrayList getAssociatedPersons(String person)
{
ArrayList associatedPersons = new ArrayList();

for(int i=0; i<assocations.size(); i++)
{
ArrayList column = (ArrayList)assocations.get(i);

String columnPerson = (String)column.get(0);

if(person.equalsIgnoreCase(columnPerson))
{
associatedPersons.add(new AssociationBean((String)column.get(0), (String)column.get(1), (String)column.get(2)));
}
}

if(associatedPersons.isEmpty())
{
associatedPersons = null;
}

return associatedPersons;
}

/**
* Initialize a 2-dimensional array - this array serves as a mockup (in-memory) database table
*
* This data structure should contain entries for all people at the wedding and their relationships
*
* See the sample data below
*
* Fill in additional data for your problem solution
*/
public void initialize()
{
ArrayList columns = new ArrayList();
columns.add("Ann");
columns.add("dating");
columns.add("Cain");
assocations.add(columns);
}

/**
* Databean - an association for a person
*/
public class AssociationBean
{
String person1 = null;
String assocation = null;
String person2 = null;

public AssociationBean(String person1, String assocation, String person2)
{
this.person1 = person1;

this.assocation = assocation;

this.person2 = person2;
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuffer buffer = new StringBuffer();

buffer.append(person1);
buffer.append("-");
buffer.append(assocation);
buffer.append("-");
buffer.append(person2);

return buffer.toString();
}

/**
* @return Returns the assocation.
*/
public String getAssocation()
{
return assocation;
}

/**
* @param assocation The assocation to set.
*/
public void setAssocation(String assocation)
{
this.assocation = assocation;
}

/**
* @return Returns the person.
*/
public String getPerson1()
{
return person1;
}

/**
* @param person The person to set.
*/
public void setPerson1(String person1)
{
this.person1 = person1;
}

/**
* @return Returns the person.
*/
public String getPerson2()
{
return person2;
}

/**
* @param person The person to set.
*/
public void setPerson2(String person2)
{
this.person2 = person2;
}
}
}
• 02-25-2009, 08:52 AM
mtyoung
i want to know ...
how can RECURSIVE method used in?
method takes 2 inputs, and result shows relationships of each input, but cant see any relation between 2 inputs
• 02-25-2009, 04:19 PM
Steve11235
Get a piece of paper and a pencil. Draw out the table of relationships. Then, write down the steps you would follow to trace all the relationships using plain language. Indent to show the logic structure. Once you can read through what you have written down and it works, turn that into code.