943,838 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1108
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 14th, 2008
0

Re: Printing output of an array

The Family is composed of the father and mother (both class Adult ), and children(Child class)
[code]public class Adult extends Human
{
public Adult(String name, int age)
{
super(name);
this.age=age;
}
protected String getName()
{
return this.name;
}
public int getAge(int mAge)
{
return this.age=mAge;
}
public boolean canWatchMovie(String mR)
{
return true;
}
}
[code]
there is also MovieRating class
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class MovieRating
  4. {
  5. private Map ageMap = new HashMap();
  6. private String movieRating;
  7. public MovieRating()
  8. {
  9. ageMap.put("PG", 18); //parental Guidance
  10. ageMap.put("G", 4); //general viewing
  11. ageMap.put("A", 27); //Adult material
  12. }
  13. public String getRating()
  14. {
  15. return movieRating;
  16. }
  17.  
  18. public boolean getStatus(String rating, int age)
  19. {
  20. int minAge = 0;
  21.  
  22. if(rating.equalsIgnoreCase("PG"))
  23. {
  24. minAge = getPGuidance();
  25. }
  26. else if (rating.equalsIgnoreCase("G"))
  27. {
  28. minAge = getGeneral();
  29. }
  30. else if (rating.equalsIgnoreCase("A"))
  31. {
  32. minAge = getAdult();
  33. }
  34.  
  35. if(age >= minAge)
  36. {
  37. return true;
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43.  
  44. }
  45. public int getPGuidance()
  46. {
  47. // Retrieve the minimum age allowed to watch movie rated PG.
  48. return (Integer)ageMap.get("PG");
  49. }
  50. public int getGeneral()
  51. {
  52. // Retrieve the age allowed to watch movie rated G.
  53. return (Integer)ageMap.get("G");
  54. }
  55. public int getAdult()
  56. {
  57. // Retrieve the age allowed to watch movie rated Adult.
  58. return (Integer)ageMap.get("A");
  59. }
  60.  
  61. public Object getValue()
  62. {
  63. // return ageMap.values();
  64. return ageMap.toString();
  65. }
  66. }

Child class
Java Syntax (Toggle Plain Text)
  1. public class Child extends Human
  2. {
  3. int childAge;
  4. MovieRating mRts = new MovieRating();
  5. String mrating;
  6.  
  7. public Child(String name, int age)
  8. {
  9. super(name);
  10. this.age=age;
  11. }
  12.  
  13. public boolean isObjectEmpty(Object obj)
  14. {
  15. boolean result = obj==null;
  16. return result;
  17. }
  18.  
  19. public void setAge(int age)
  20. {
  21. childAge = age;
  22. }
  23.  
  24. public int getAge(int mAge)
  25. {
  26. return this.age=mAge;
  27. }
  28. public String getName()
  29. {
  30. return this.name;
  31. }
  32. public boolean canWatchMovie(String mR)
  33. {
  34. return mRts.getStatus(this.mrating , this.age);
  35. }
  36. }


So the getMovieGoers method take rating as a parameter and the rating is checked by the MovieRating class, that's why I have something like this getMovieGoers(mRt.getGeneral) for movies rated G and getMovieGoers(mRt.getAdult) for movies rated A.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
janamrob is offline Offline
22 posts
since Sep 2008
Nov 14th, 2008
0

Re: Printing output of an array

and what's in Human.java?
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Nov 14th, 2008
0

Re: Printing output of an array

just a couple of remarks:

what's the use of setting getName() in adult as protected?
also, just check the two codes for Child.java I post under this, the first one is the one you posted, the second one is (IMO) a bit more correct

Java Syntax (Toggle Plain Text)
  1. //Your current code
  2. public class Child extends Human
  3. {
  4. int childAge;
  5. MovieRating mRts = new MovieRating();
  6. String mrating;
  7.  
  8. public Child(String name, int age)
  9. {
  10. super(name);
  11. this.age=age;
  12. }
  13.  
  14. public boolean isObjectEmpty(Object obj)
  15. {
  16. boolean result = obj==null;
  17. return result;
  18. }
  19.  
  20. public void setAge(int age)
  21. {
  22. childAge = age;
  23. }
  24.  
  25. public int getAge(int mAge)
  26. {
  27. return this.age=mAge;
  28. }
  29. public String getName()
  30. {
  31. return this.name;
  32. }
  33. public boolean canWatchMovie(String mR)
  34. {
  35. return mRts.getStatus(this.mrating , this.age);
  36. }
  37. }

Java Syntax (Toggle Plain Text)
  1. //My revision
  2. public class Child extends Human
  3. {
  4. int childAge;
  5. MovieRating mRts = new MovieRating();
  6. String mrating;
  7.  
  8. public Child(String name, int age)
  9. {
  10. super(name);
  11. this.childAge=age;
  12. }
  13. // I'm not sure what this is supposed to do. is it supposed to check the current object?
  14. // if not, it doesn't really belong in Child.java
  15. public boolean isObjectEmpty(Object obj)
  16. {
  17. boolean result = obj==null;
  18. return result;
  19. }
  20.  
  21. public void setAge(int age)
  22. {
  23. this.childAge = age;
  24. }
  25.  
  26. public int getAge() // No argument needed for a getter, the data's already here
  27. {
  28. return childAge;
  29. }
  30. public String getName()
  31. {
  32. return this.name;
  33. }
  34. public boolean canWatchMovie(String mR)
  35. {
  36. return mRts.getStatus(this.mrating , this.childAge);
  37. }
  38. }
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Nov 14th, 2008
0

Re: Printing output of an array

I had a couple of moments to spare, so I solved this one for you..
you did get (or should have gotten) an error message, namely
>> Adult cannot be cast to java.lang.String
and this for every time you tried to print. you were trying to cast the element of your arrayList (which was either Adult or Child of type) to a String.
I gave you a concept to follow, not the entire sollution. I'll hereby post your entire code (as I think it is) with my changes in them.

ow yeah, I commented the dog, rabbit and all that, since I don't have those classes, and didn't think they were really relevant for this exercise

take a look at it and ask back if you don't see the difference:

Java Syntax (Toggle Plain Text)
  1. //Family.java
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.io.*;
  5. public class Family
  6. {
  7.  
  8. private Adult father;
  9. private Adult mother;
  10. private Child[] children= {new Child("Frank",10), new Child("Lisa",18)};;
  11. /*
  12.   private Dog ourDog;
  13.   private Cats ourCat;
  14.   private Rabbit ourRabbit; */
  15. private static ArrayList mGoers = new ArrayList();
  16.  
  17. // int Age;
  18.  
  19. public Family()
  20. {
  21.  
  22. }
  23.  
  24. public Family(Adult father, Adult mother, Child[] children)
  25. {
  26. // setFather(father);
  27. // setMother(mother);
  28. // setChildren(children);
  29. /* ourDog=new Dog("");
  30.   ourCat=new Cats("");
  31.   ourRabbit = new Rabbit("");*/
  32. }
  33.  
  34. // private Adult getFather()
  35. // {
  36. // return this.father;
  37. // }
  38. //
  39. // private Adult getMother()
  40. // {
  41. // return this.mother;
  42. // }
  43. //
  44. // private Child[] getChildren()
  45. // {
  46. // return this.children;
  47. // }
  48. //
  49.  
  50. // private void setFather(Adult father)
  51. // {
  52. // this.father = father;
  53. // }
  54. //
  55. // private void setMother(Adult mother)
  56. // {
  57. // this.mother = mother;
  58. // }
  59. //
  60. // private void setChildren(Child[] children)
  61. // {
  62. // this.children = children;
  63. // }
  64.  
  65. public Collection getMovieGoers(int rating)
  66. {
  67. father = new Adult("John",42);
  68. mother = new Adult("Laura",36);
  69. //I consider rating here to be the starting age that is allowed for the movie
  70. if ( father.getAge() >= rating)
  71. mGoers.add(father);
  72. if ( mother.getAge() >= rating)
  73. mGoers.add(mother);
  74. for(int i = 0; i < children.length; i++)
  75. {
  76. if (children[i].getAge() >= rating)
  77. mGoers.add(children[i]);
  78. }
  79. return mGoers;
  80. }
  81.  
  82. public static void main(String args[])throws IOException
  83. {
  84. MovieRating mRt = new MovieRating();
  85. // Scenario I : Movies rated G
  86. try
  87. {
  88. Family f = new Family();
  89. ArrayList mgGoers= (ArrayList)f.getMovieGoers(mRt.getGeneral());
  90. String persons = "";
  91. for ( int i = 0; i < mgGoers.size(); i++)
  92. {
  93. String name = "";
  94. if ((mgGoers.get(i)) instanceof Adult){
  95. Adult a = (Adult)mgGoers.get(i);
  96. name = a.getName();
  97. }
  98. else{
  99. Child b = (Child)mgGoers.get(i);
  100. name = b.getName();
  101. }
  102.  
  103. if ( persons.equals(""))
  104. {
  105. persons += name;
  106. }
  107. else
  108. {
  109. persons += ", " + name;
  110. }
  111. }
  112. System.out.println("The next people can go watch the movie: " + persons);
  113.  
  114. }
  115. catch(Exception e)
  116. {
  117. System.out.println(e.getMessage());
  118. }
  119.  
  120. // Scenario II : Movies rated PG
  121. try
  122. {
  123. Family f = new Family();
  124. ArrayList mpgGoers= (ArrayList)f.getMovieGoers(mRt.getPGuidance());
  125. String persons = "";
  126. for ( int i = 0; i < mpgGoers.size(); i++)
  127. {
  128. String name = "";
  129. if ((mpgGoers.get(i)) instanceof Adult){
  130. Adult a = (Adult)mpgGoers.get(i);
  131. name = a.getName();
  132. }
  133. else{
  134. Child b = (Child)mpgGoers.get(i);
  135. name = b.getName();
  136. }
  137.  
  138. if ( persons.equals(""))
  139. {
  140. persons += name;
  141. }
  142. else
  143. {
  144. persons += ", " + name;
  145. }
  146. }
  147. System.out.println("The next people can go watch the movie: " + persons);
  148.  
  149. }
  150. catch(Exception e)
  151. {
  152. System.out.println(e.getMessage());
  153. }
  154. // Scenario III : Movies rated A
  155. try
  156. {
  157. Family fam = new Family();
  158. ArrayList maGoers= (ArrayList)fam.getMovieGoers(mRt.getAdult());
  159. String persons = "";
  160. for ( int i = 0; i < maGoers.size(); i++)
  161. {
  162. String name = "";
  163. if ((maGoers.get(i)) instanceof Adult){
  164. Adult a = (Adult)maGoers.get(i);
  165. name = a.getName();
  166. }
  167. else{
  168. Child b = (Child)maGoers.get(i);
  169. name = b.getName();
  170. }
  171.  
  172. if ( persons.equals(""))
  173. {
  174. persons += name;
  175. }
  176. else
  177. {
  178. persons += ", " + name;
  179. }
  180. }
  181. System.out.println("The next people can go watch the movie: " + persons);
  182.  
  183. }
  184. catch(Exception e)
  185. {
  186. System.out.println(e.getMessage());
  187. }
  188. finally
  189. {
  190. System.out.println("End");
  191. // Family fam = new Family();
  192. // fam.ourCat.catType("Angora");
  193. // fam.ourCat.setCatColor("Brownish white");
  194. // fam.ourDog.dogType("African Shepherd Dog");
  195. // fam.ourDog.setDogColor("Black");
  196. // fam.ourRabbit.setRabbitColor("White");
  197. // fam.ourRabbit.rabbitType("Akorino");
  198. //
  199. // System.out.println("We own the following pets: " +
  200. // " " + fam.ourDog.MydogType+ " " + fam.ourDog.dog_color + " in color;" +
  201. // " a small " + fam.ourCat.cat_color + " " +fam.ourCat.MyCatType+ " cat;"+
  202. // " and a "+ fam.ourRabbit.rabbit_color +" "+ fam.ourRabbit.MyRabbitType +
  203. // " rabbit.");
  204.  
  205. }
  206. }
  207. }

Java Syntax (Toggle Plain Text)
  1. //Human.java
  2.  
  3. public class Human {
  4.  
  5. protected String name;
  6.  
  7. public Human(String name){
  8. setName(name);
  9. }
  10.  
  11. public void setName(String name){
  12. this.name = name;
  13. }
  14. public String getName(){
  15. return this.name;
  16. }
  17.  
  18. }

Java Syntax (Toggle Plain Text)
  1. //Adult.java
  2. public class Adult extends Human
  3. {
  4. private int aAge;
  5. public Adult(String name, int age)
  6. {
  7. super(name);
  8. setAge(age);
  9. }
  10. @Override
  11. public String getName()
  12. {
  13. return this.name;
  14. }
  15. public int getAge(){
  16. return aAge;
  17. }
  18. public void setAge(int age){
  19. this.aAge = age;
  20. }
  21. public boolean canWatchMovie(String mR)
  22. {
  23. return true;
  24. }
  25. }

Java Syntax (Toggle Plain Text)
  1. //Child.java
  2. public class Child extends Human
  3. {
  4. int childAge;
  5. MovieRating mRts = new MovieRating();
  6. String mrating;
  7.  
  8. public Child(String name, int age)
  9. {
  10. super(name);
  11. this.childAge=age;
  12. }
  13. /*
  14.   public boolean isObjectEmpty(Object obj)
  15.   {
  16.   boolean result = obj==null;
  17.   return result;
  18.   }
  19. */
  20. public void setAge(int age)
  21. {
  22. this.childAge = age;
  23. }
  24.  
  25. public int getAge()
  26. {
  27. return childAge;
  28. }
  29.  
  30. @Override
  31. public String getName()
  32. {
  33. return this.name;
  34. }
  35. public boolean canWatchMovie(String mR)
  36. {
  37. return mRts.getStatus(this.mrating , this.childAge);
  38. }
  39. }

Java Syntax (Toggle Plain Text)
  1. //MovieRating.java
  2. import java.util.*;
  3.  
  4. public class MovieRating
  5. {
  6. private Map ageMap = new HashMap();
  7. private String movieRating;
  8. public MovieRating()
  9. {
  10. ageMap.put("PG", 18); //parental Guidance
  11. ageMap.put("G", 4); //general viewing
  12. ageMap.put("A", 27); //Adult material
  13. }
  14. public String getRating()
  15. {
  16. return movieRating;
  17. }
  18.  
  19. public boolean getStatus(String rating, int age)
  20. {
  21. int minAge = 0;
  22.  
  23. if(rating.equalsIgnoreCase("PG"))
  24. {
  25. minAge = getPGuidance();
  26. }
  27. else if (rating.equalsIgnoreCase("G"))
  28. {
  29. minAge = getGeneral();
  30. }
  31. else if (rating.equalsIgnoreCase("A"))
  32. {
  33. minAge = getAdult();
  34. }
  35.  
  36. if(age >= minAge)
  37. {
  38. return true;
  39. }
  40. else
  41. {
  42. return false;
  43. }
  44.  
  45. }
  46. public int getPGuidance()
  47. {
  48. // Retrieve the minimum age allowed to watch movie rated PG.
  49. return (Integer)ageMap.get("PG");
  50. }
  51. public int getGeneral()
  52. {
  53. // Retrieve the age allowed to watch movie rated G.
  54. return (Integer)ageMap.get("G");
  55. }
  56. public int getAdult()
  57. {
  58. // Retrieve the age allowed to watch movie rated Adult.
  59. return (Integer)ageMap.get("A");
  60. }
  61.  
  62. public Object getValue()
  63. {
  64. // return ageMap.values();
  65. return ageMap.toString();
  66. }
  67. }
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007

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: coding assistance
Next Thread in Java Forum Timeline: Weird Question





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


Follow us on Twitter


© 2011 DaniWeb® LLC