Results 1 to 8 of 8
  1. #1
    sony1 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default which classes to write..how to program this?

    11. Create a class called Families which creates a one-to-many mapping of parent name (String) to Child objects. Create any needed member variables and write the two specified methods.

    class Families {


    public void addToFamily( String parent, Child child) {


    }

    public List<Child> getChildren( String parent) {


    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for posting your assignment. I assume that you are going to show the fruits of your efforts and then ask a direct question, correct?

  3. #3
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    Any exception text?

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by sony1 View Post
    11. Create a class called Families which creates a one-to-many mapping of parent name (String) to Child objects. Create any needed member variables and write the two specified methods.

    class Families {


    public void addToFamily( String parent, Child child) {


    }

    public List<Child> getChildren( String parent) {


    }
    }
    mapping maybe you need a HashMap ?? just a thought

  5. #5
    sony1 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default why a null pointer exception?

    import java.util.HashMap;
    import java.util.List;

    class Families {

    HashMap<String,List<String>> hm=new HashMap<String,List<String>>();

    void find(String parent,String child)
    {

    List<String> l = hm.get(parent);
    if(l.isEmpty()== true)
    {
    List list = null;
    list.add(child);
    hm.put(parent, list);
    }

    else
    {
    // parent already exists
    l.add(child);
    }
    }
    //public void addToFamily( String parent, Child child)
    //{
    //a=find(parent);

    //}


    public List<String> getChildren( String parent)
    {
    List l=null;
    l=hm.get(parent);
    return l;
    }

    public static void main(String args[])
    {Families ob=new Families();

    ob.find("nav","khushi");
    ob.find("nav", "soni");
    ob.find("nav", "vish");
    ob.find("ash", "sonu");
    List li=null;

    li=ob.getChildren("naveen");
    System.out.println(li);



    }
    }

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sony1 View Post
    Java Code:
    import java.util.HashMap;
    import java.util.List;
    
    class Families {
    	
    	HashMap<String,List<String>> hm=new HashMap<String,List<String>>();
    
    	void find(String parent,String child)
    	{
    		
    	List<String> l = hm.get(parent);
    	if(l.isEmpty()== true)
    What if 'parent' isn't present (yet) in your map? Most certainly 'l' will be null.

    kind regards,

    Jos

  7. #7
    sony1 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    how to get rid of this error then?
    also the question was this..
    i m suppose to vreate parent child mapping
    how m i suppose to cretae the parent and child class?
    this is not an assignment question as pointed out by the moderator.
    It was asked in one of the interviews..and i ws nt able to answer it.


    class Families {



    public void addToFamily( String parent, Child child) {



    }

    public List<Child> getChildren( String parent) {


    }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Your design should allow for a person NOT to have children.
    And when creating a child, the parents MUST be given to the Child's Constructor.

Similar Threads

  1. How to write two server program in java
    By priyamurugar in forum Networking
    Replies: 1
    Last Post: 04-27-2010, 11:20 AM
  2. Replies: 0
    Last Post: 09-22-2009, 07:15 PM
  3. is it possible to write program with out thread
    By makpandian in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-21-2008, 05:41 PM
  4. How to write interceptor program in struts2?
    By vasu in forum Web Frameworks
    Replies: 1
    Last Post: 10-07-2008, 07:53 AM
  5. need help to write Program in JAVA
    By maneibr in forum New To Java
    Replies: 1
    Last Post: 03-13-2008, 01:28 PM

Posting Permissions

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