Results 1 to 3 of 3
  1. #1
    kj2009 is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default 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;
    }
    }
    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Replies: 3
    Last Post: 09-27-2008, 04:46 AM
  2. exercise of recursive method
    By amexudo in forum New To Java
    Replies: 2
    Last Post: 03-09-2008, 05:55 PM
  3. Replies: 1
    Last Post: 02-16-2008, 09:10 PM
  4. Replies: 2
    Last Post: 02-16-2008, 08:52 PM
  5. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •