Results 1 to 3 of 3
Thread: Java Recursive method problem
- 02-25-2009, 05:41 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
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, 07:52 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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, 03:19 PM #3
Similar Threads
-
increase stack size in eclipse for highly recursive method.
By Daedalus in forum EclipseReplies: 3Last Post: 09-27-2008, 04:46 AM -
exercise of recursive method
By amexudo in forum New To JavaReplies: 2Last Post: 03-09-2008, 05:55 PM -
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02-16-2008, 09:10 PM -
Recursive Method ==> find how many times a value is repeated in an array
By NatNat in forum New To JavaReplies: 2Last Post: 02-16-2008, 08:52 PM -
Recursive Method
By bluegreen7hi in forum New To JavaReplies: 5Last Post: 11-29-2007, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks