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

    Default what wrong with my java code

    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());
        }
    }
    Person class
    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/familySize();
                }
    }
    the result should be
    My family size: 8
    My family's average age: 17.125

    but i got
    My family size: 8
    My family's average age: 9.635416666666668

    the sum of age is correct 137
    but how can i divide it for get 17.125
    What wrong with my code?
    Last edited by MAXZZXAM; 11-19-2009 at 07:48 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Please format your code and post it using code tags
    to make it easier for those trying to help you.
    Also do your own debugging by putting System.out.println statements to see
    the values of certain variables at critical points in the code. You learn faster
    that way.

  3. #3
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    first of all //main is sufficient
    second of all //code is sufficient

    Chill out on the forward slashes

    to post code use the post code tags [ c o d e ] paste code in here and eliminate the spaces in my tags [/c o d e ]
    Last edited by aaroncarpet; 11-19-2009 at 12:32 AM.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Your calculations in the averageFamilyAge method are strange.
    Create a getSumOfAge method (you said you have this value correct).
    Then simply reuse the methods you have created already by doing averageAge =getSumOfAge()/familySize() .

Similar Threads

  1. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 06:44 PM
  2. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 02:50 AM
  3. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 12:26 PM
  4. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 AM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 AM

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
  •