943,548 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 769
  • Java RSS
Oct 27th, 2008
0

Arrays in Family class

Expand Post »
Hi, I have a small question regarding arrays. I have the class Family below:
Java Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.io.*;
  4. public class Family
  5. {
  6.  
  7. private Adult father;
  8. private Adult mother;
  9. private Child[] children;
  10.  
  11. private Dog ourDog;
  12. private Cats ourCat;
  13. private Rabbit ourRabbit;
  14.  
  15. int Age;
  16.  
  17. public Family()
  18. {
  19.  
  20. }
  21.  
  22. public Family(Adult father, Adult mother, Child[] children)
  23. {
  24. setFather(father);
  25. setMother(mother);
  26. setChildren(children);
  27. ourDog=new Dog("");
  28. ourCat=new Cats("");
  29. ourRabbit = new Rabbit("");
  30. }
  31.  
  32. private Adult getFather()
  33. {
  34. return this.father;
  35. }
  36.  
  37. private Adult getMother()
  38. {
  39. return this.mother;
  40. }
  41.  
  42. private Child[] getChildren()
  43. {
  44. return this.children;
  45. }
  46.  
  47. private void setFather(Adult father)
  48. {
  49. this.father = father;
  50. }
  51.  
  52. private void setMother(Adult mother)
  53. {
  54. this.mother = mother;
  55. }
  56.  
  57. private void setChildren(Child[] children)
  58. {
  59. this.children = children;
  60. }
  61.  
  62. public Collection getMovieGoers(int rating)
  63. {
  64. ArrayList mGoers = new ArrayList();
  65. // I consider rating here to be the starting age that is allowed for the movie
  66. if ( father.getAge() >= rating)
  67. mGoers.add(father);
  68. if ( mother.getAge() >= rating)
  69. mGoers.add(mother);
  70. for(int i = 0; i < children.length; i++)
  71. {
  72. if (children[i].getAge() >= rating)
  73. mGoers.add(children[i]);
  74. }
  75. return mGoers;
  76.  
  77. }
  78. public static void main(String args[])throws IOException
  79. {
  80. MovieRating mRt = new MovieRating();
  81.  
  82. Child[] children = {new Child("Frank",10), new Child("Lisa",18)};
  83. Family fam = new Family(new Adult("John",37), new Adult("Mary",35),children);
  84. ArrayList mvGoers= (ArrayList)fam.getMovieGoers(mRt.getAdult());
  85.  
  86. System.out.println("Movie goers: "+mvGoers+"");
  87.  
  88. //pets
  89. fam.ourCat.catType("Angora");
  90. fam.ourCat.setCatColor("Brownish white");
  91. fam.ourDog.dogType("African Shepherd Dog");
  92. fam.ourDog.setDogColor("Black");
  93. fam.ourRabbit.setRabbitColor("White");
  94. fam.ourRabbit.rabbitType("Akorino");
  95.  
  96. System.out.println("We own the following pets: " +
  97. " " + fam.ourDog.MydogType+ " " + fam.ourDog.dog_color + " in color;" +
  98. " a small " + fam.ourCat.cat_color + " " +fam.ourCat.MyCatType+ " cat;"+
  99. " and a "+ fam.ourRabbit.rabbit_color +" "+ fam.ourRabbit.MyRabbitType +
  100. " rabbit.");
  101. }
  102. }

In the constructor for Family(Adult father, Adult mother, Child[] children), I have father, mother (both Adult) and Child[] which is an array of chidren.
The problem is in the main method:
Java Syntax (Toggle Plain Text)
  1. Child[] children = {new Child("Frank",10), new Child("Lisa",18)};
  2. Family fam = new Family(new Adult("John",37), new Adult("Mary",35),children);
  3. ArrayList mvGoers= (ArrayList)fam.getMovieGoers(mRt.getAdult());
  4.  
  5. System.out.println("Movie goers: "+mvGoers+"");

When I run the application this is what I get in the last line
Java Syntax (Toggle Plain Text)
  1. Movie goers: [myinheritancepolymorph.Adult@1a46e30, myinheritancepolymorph.Adult@3e25a5]

any help on how to modify the code so that it can print out all the family members.
NOTE: Other sections have no problem except this array section.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 27th, 2008
0

Re: Arrays in Family class

Whenever in need of a sane string representation of your instance, override the toString() method for the given class. This makes sure that each class is responsible for giving out its own representation without having to write the same temporary code again and again. For pretty printing arrays, convert them first to a list using Arrays.asList(yourArray) .
Java Syntax (Toggle Plain Text)
  1. public class Family {
  2. private Adult father;
  3. private Adult mother;
  4. private Child[] children;
  5.  
  6. public String toString() {
  7. StringBuilder buf = new StringBuilder(256);
  8. buf.append("Father {").append(father)
  9. .append("} Mother {").append(mother)
  10. .append("} Children {").append(Arrays.asList(children))
  11. .append("}");
  12. return buf.toString();
  13. }
  14.  
  15. }
  16.  
  17. public class Adult {
  18. private String name;
  19. // other instance variables go here
  20.  
  21. public String toString() {
  22. StringBuilder buf = new StringBuilder(128);
  23. buf.append("Name: ").append(name);
  24. return buf.toString();
  25. }
  26.  
  27. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 27th, 2008
0

Re: Arrays in Family class

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
Whenever in need of a sane string representation of your instance, override the toString() method for the given class. This makes sure that each class is responsible for giving out its own representation without having to write the same temporary code again and again. For pretty printing arrays, convert them first to a list using Arrays.asList(yourArray) .
Java Syntax (Toggle Plain Text)
  1. public class Family {
  2. private Adult father;
  3. private Adult mother;
  4. private Child[] children;
  5.  
  6. public String toString() {
  7. StringBuilder buf = new StringBuilder(256);
  8. buf.append("Father {").append(father)
  9. .append("} Mother {").append(mother)
  10. .append("} Children {").append(Arrays.asList(children))
  11. .append("}");
  12. return buf.toString();
  13. }
  14.  
  15. }
  16.  
  17. public class Adult {
  18. private String name;
  19. // other instance variables go here
  20.  
  21. public String toString() {
  22. StringBuilder buf = new StringBuilder(128);
  23. buf.append("Name: ").append(name);
  24. return buf.toString();
  25. }
  26.  
  27. }
aah! its seems we're not communicating! You are complicating the code further. My question was how to modify this part of my code
Java Syntax (Toggle Plain Text)
  1. Child[] children = {new Child("Frank",10), new Child("Lisa",18)};
  2. Family fam = new Family(new Adult("John",37), new Adult("Mary",35),children);
  3. ArrayList mvGoers= (ArrayList)fam.getMovieGoers(mRt.getAdult());
  4.  
  5. System.out.println("Movie goers: "+mvGoers+"");

to get an output like this:
Movie goers: [John , Mary, Frank, Lisa]

as in the names of Adult and Child given.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 27th, 2008
0

Re: Arrays in Family class

> aah! its seems we're not communicating! You are complicating the code further

Yes, rightly said, because it is what you asked for in the first place. Re-read your first post:
Quote ...
any help on how to modify the code so that it can print out all the family members.
> NOTE: Other sections have no problem except this array section.

There is no `array section' here; it is how the toString() to the ArrayList class is implemented.

There are two ways of doing it; either the way mentioned in my first post or writing a method which takes the List , loops over it and extracts the name, either printing them or adding them again to a new List which then gets displayed.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 27th, 2008
0

Re: Arrays in Family class

Click to Expand / Collapse  Quote originally posted by Achupa ...
to get an output like this:
Movie goers: [John , Mary, Frank, Lisa]

as in the names of Adult and Child given.
your code works and does exactly what you programmed it to do. just as ~s.o.s~ pointed out, whenever you try to print an Object or a list of Objects, you'll call the toString method of the class.

since your Family class does not have such a method, you'll just see a String representation of the memory-address used by the Object. if you want to make a print-out of all the persons' names in the list, the best way to do so is by creating that toString method.
Reputation Points: 919
Solved Threads: 352
Nearly a Posting Maven
stultuske is offline Offline
2,471 posts
since Jan 2007
Oct 27th, 2008
0

Re: Arrays in Family class

thanks, i've corrected it.

I have another problem with a List in a class called Mammal. The code is like this:
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. public abstract class Mammal
  3. {
  4. Head head;
  5. char gender;
  6. public String name;
  7. protected int age = 0;
  8. private List <Limb> limbs;
  9. String myArm,myLeg;
  10.  
  11. protected Mammal()
  12. {
  13. this.limbs = new ArrayList<Limb>();
  14. limbs.add(new Leg("Left"));
  15. limbs.add(new Leg("Right"));
  16. limbs.add(new Arms("Left"));
  17. limbs.add(new Arms("Right"));
  18. }
  19.  
  20. protected Mammal(List<Limb> limbs)
  21. {
  22. this.limbs = limbs;
  23. }
  24.  
  25. // adding limbs:
  26. protected void addLimbs(List<Limb>limbToAdd)
  27. {
  28. this.limbs.add((Limb) limbToAdd);
  29.  
  30. }
  31.  
  32. //remove limbs
  33. protected void removeLimbs(List<Limb> limbToRemove)
  34. {
  35. Object temp = null;
  36. Iterator iter = limbs.iterator();
  37. while (iter.hasNext())
  38. {
  39. temp = iter.next();
  40. if (temp instanceof Arms)
  41. {
  42. //myArm = (Arms)temp;
  43. iter.remove(); // iter.remove(myArm);
  44. if (temp instanceof Leg)
  45. {
  46. //myLeg = (Leg)temp;
  47. iter.remove(); //iter.remove(myLeg);
  48. }
  49. }
  50. }
  51. }
  52. protected List<Limb> getLimbs()
  53. {
  54. return this.limbs;
  55. }
  56.  
  57. public Mammal(String name)
  58. {
  59. this.name = name;
  60. }
  61.  
  62. public void getOlder(int years)
  63. {
  64. age += years;
  65. }
  66.  
  67. public void makeSound()
  68. {
  69. System.out.print(name+" Oooops ");
  70. }
  71.  
  72. public void eat()
  73. {
  74. System.out.print("Some food please...");
  75. }
  76.  
  77. }

How do I instatiate the limbs list?
Since in my implemetetion above my tutor tells me that I can't call its add() method
- You are trying to cast a List into a Limb. this cant work.

Java Syntax (Toggle Plain Text)
  1. // adding limbs:
  2. protected void addLimbs(List<Limb>limbToAdd)
  3. {
  4. this.limbs.add((Limb) limbToAdd);
  5.  
  6. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 27th, 2008
0

Re: Arrays in Family class

Use the addAll method of the ArrayList class; it's a good idea to consult the documentation once in a while, at least for the classes which you plan on using.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 27th, 2008
0

Re: Arrays in Family class

Looking at my code, is this comment true:

You have not instatiated the limbs list, how can you then call its add() method
- You are trying to cast a List into a Limb. this cant work.


and if so how can I correct it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 27th, 2008
0

Re: Arrays in Family class

> - You are trying to cast a List into a Limb. this cant work.

Yes, it's true; look at my previous post for the answer.

> You have not instatiated the limbs list, how can you then call its add() method

This comment seems weird since you have instantiated the ` limbs ' list in the Mammal constructor.

BTW, your implementation of removeLimbs is broken. Why do you require instanceof checks? Plus, those nested IF blocks are incorrect. You could have simply done a limbs.clear() and got away with it.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: <identifier> expected? Pls help my code!
Next Thread in Java Forum Timeline: Creating methods with Scenarios





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC