Arrays in Family class

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Arrays in Family class

 
0
  #1
Oct 27th, 2008
Hi, I have a small question regarding arrays. I have the class Family below:
  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:
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Arrays in Family class

 
0
  #2
Oct 27th, 2008
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) .
  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. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: Arrays in Family class

 
0
  #3
Oct 27th, 2008
Originally Posted by ~s.o.s~ View Post
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) .
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Arrays in Family class

 
0
  #4
Oct 27th, 2008
> 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:
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Arrays in Family class

 
0
  #5
Oct 27th, 2008
Originally Posted by Achupa View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: Arrays in Family class

 
0
  #6
Oct 27th, 2008
thanks, i've corrected it.

I have another problem with a List in a class called Mammal. The code is like this:
  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.

  1. // adding limbs:
  2. protected void addLimbs(List<Limb>limbToAdd)
  3. {
  4. this.limbs.add((Limb) limbToAdd);
  5.  
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Arrays in Family class

 
0
  #7
Oct 27th, 2008
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: Arrays in Family class

 
0
  #8
Oct 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Arrays in Family class

 
0
  #9
Oct 27th, 2008
> - 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC