/*
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. Additionally, a lot of
them may know each other in one or more manner. For example, let's take Ann
and David, who meet by the coffee machine.
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. You must show all associations between
these two people - direct and indirect.
----- Make sure to write a brief description of your solution. -----
----- Discuss conditions that can produce non-terminating recursion. -----
*/
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;
}
}
}
jksaheta 0 Newbie Poster
sillyboy 43 Practically a Master Poster
BestJewSinceJC 700 Posting Maven
verruckt24 438 Posting Shark
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.