Results 1 to 4 of 4
Thread: what wrong with my java code
- 11-18-2009, 07:32 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
what wrong with my java code
main
Person classJava 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()); } }
the result should beJava 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(); } }
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 06:48 AM.
- 11-18-2009, 07:49 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 11-18-2009, 11:25 PM #3
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-18-2009 at 11:32 PM.
- 11-19-2009, 09:38 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
What's wrong with this code?
By Doctor Cactus in forum New To JavaReplies: 4Last Post: 11-29-2008, 05:44 PM -
What is wrong with this code
By rosh72851 in forum New To JavaReplies: 13Last Post: 10-31-2008, 01:50 AM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks