Results 1 to 2 of 2

Thread: I need help

  1. #1
    MAXZZXAM is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default I need help

    main
    Java Code:
    package question2;
    public class Main{
        public Main(){
        }
        public static void main(String[] args){
            Person grandchild1 = new Person("a", 2, new Person[0]);
            Person grandchild2 = new Person("b", 3, new Person[0]);
            Person grandchild3 = new Person("c", 7, new Person[0]);
            Person grandchild4 = new Person("d", 5, new Person[0]);
            Person grandchild5 = new Person("e", 4, new Person[0]);
            Person child1 = new Person("f", 35, new Person[]{grandchild1, grandchild2, grandchild3});
            Person child2 = new Person("g", 28, new Person[]{grandchild4, grandchild5});
            Person me = new Person("h", 53, new Person[]{child1, child2});
            Person neighbor = new Person("x", 9999, new Person[0]);
            System.out.println("My family size: " + me.familySize());
            System.out.println("My family's average age: " + me.averageFamilyAge());
        }
    }
    code
    Java Code:
    package question2;
    public class Person
    
    {
    private String name;
    private double age;
    private Person[] children;
    
    
        public Person(String name,double age,Person[] children)
                {
                this.name=name;
                this.age=age;
                for(int i=0;i<children.length;i++)
                this.children=children;
                }
    
        public int familySize ()
                {
                int familycount=0;
                    if(children==null)
                    { return 1; }
                    else
                    {   for(int i=0;i<children.length;i++)
                        {  familycount=familycount+children[i].familySize(); }
                    }
                return familycount+1;
                }
        public double averageFamilyAge()
                {
                double sumage=0;
                if(children==null)
                    {return age;}
                else
                {
                    for(int i=0;i<children.length;i++)
                    {sumage=sumage+(children[i].averageFamilyAge());}
    
                    sumage=(sumage+(age));
                }
                 return sumage;
                }
       
    }
    how can i get
    averageFamilyAge() divide by familySize()
    toget the answer

    My family size: 8
    My family's average age: 17.125

  2. #2
    MAXZZXAM is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    O.K. already got answer
    [code]package question2;
    public class Main{
    public Main(){
    }
    public static void main(String[] args){
    Person grandchild1 = new Person("a", 2, new Person[0]);
    Person grandchild2 = new Person("b", 3, new Person[0]);
    Person grandchild3 = new Person("c", 7, new Person[0]);
    Person grandchild4 = new Person("d", 5, new Person[0]);
    Person grandchild5 = new Person("e", 4, new Person[0]);
    Person child1 = new Person("f", 35, new Person[]{grandchild1, grandchild2, grandchild3});
    Person child2 = new Person("g", 28, new Person[]{grandchild4, grandchild5});
    Person me = new Person("h", 53, new Person[]{child1, child2});
    Person neighbor = new Person("x", 9999, new Person[0]);
    System.out.println("My family size: " + me.familySize());
    System.out.println("My family's average age: " + me.averageFamilyAge());
    }
    }
    [code/]
    code
    [code]
    //Name : Mr.Rachan Limcharoen
    //ID : 5122780331
    package question2;
    public class Person
    { private String name;
    private double age;
    private Person[] children;
    public Person(String name,double age,Person[] children)
    { this.name=name;
    this.age=age;
    this.children=children;
    }
    public int familySize ()
    { int familycount=0;
    if(children==null)
    {
    return 1;
    }
    else
    { for(int i=0;i<children.length;i++)
    {
    familycount=familycount+children[i].familySize();
    }
    }
    return familycount+1;
    }

    public double sumAge()
    { double sumage=0;
    if(children==null)
    {
    return age;
    }
    else
    { for(int i=0;i<children.length;i++)
    {
    sumage=sumage+(children[i].sumAge());
    }
    sumage=(sumage+(age));
    }
    return sumage;
    }
    public double averageFamilyAge()
    {
    return sumAge()/familySize();
    }
    }
    [code/]

Tags for this Thread

Posting Permissions

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