943,806 Members | Top Members by Rank

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

Re: Creating and Using Exceptions

look at the method below
Java Syntax (Toggle Plain Text)
  1. public void addMovieGoers(Human h) throws IllegalAgeException
  2. {
  3. Human a = new Adult("Joe",43);
  4. Human b = new Adult("Sue",39);
  5. Human c = new Adult("Tracy",20);
  6. Human d = new Child("Sammy",17);
  7. Human e = new Child("Julie",12);
  8. Human f = new Child("Mona",6);
  9. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  10. hm.add(e);hm.add(f);
  11.  
  12. hm.trimToSize();
  13. String toPrint = "";
  14.  
  15. if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
  16. {
  17. if(mrPG.equals(movieNew.getRating())) //movies rated PG
  18. {
  19. System.out.println("The following can only watch under parental guidance:");
  20. for(int i = 0; i < hm.size(); i++)
  21. {
  22. Human test = (Human)hm.get(i);
  23. if ( test instanceof Child)
  24. {
  25. if(toPrint.equals(""))
  26. {
  27. System.out.println(test);
  28. }
  29. else
  30. {
  31. System.out.println("Children: " + toPrint);
  32. }
  33. }
  34. }
  35. }
  36. else if(mrA.equals(movieNew.getRating())) //movies rated Adult
  37. {
  38. System.out.println("The following can watch...");
  39. for ( int i = 0; i < hm.size(); i++)
  40. {
  41. Human test = (Human)hm.get(i);
  42. if ( test instanceof Adult)
  43. {
  44. if(toPrint.equals(""))
  45. {
  46. System.out.println(test);
  47. }
  48. else
  49. {
  50. System.out.println("Adults: " + toPrint);
  51. }
  52. }
  53.  
  54. }
  55. }
  56. else if(mrG.equals(movieNew.getRating()))//Movies rated G
  57. {
  58. System.out.println("This is movie rated G. So the following can watch"+
  59. ": "+hm+"");
  60. }
  61. else
  62. {
  63. throw new IllegalAgeException("You may not be old enough to watch a movie.");
  64. }
  65. }
  66.  
  67.  
  68. }

javaAddict is telling me :"You are making the same mistake with addMovieGoers. You are supposed to take the argument and add it to the ArrayList. The user calling the method will set which "humans" will "watch" the movie. And again you are using fixed values. It is as if the method has no meaning of existence since it makes no difference what the argument is since you don't use it"

So can you help me correct this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Nov 4th, 2008
0

Re: Creating and Using Exceptions

The argument: Human h is the one to be added in the ArrayList.
The way you have it. no matter what the argument is, you will always insert the same values:
Java Syntax (Toggle Plain Text)
  1. Human a = new Adult("Joe",43);
  2. Human b = new Adult("Sue",39);
  3. Human c = new Adult("Tracy",20);
  4. Human d = new Child("Sammy",17);
  5. Human e = new Child("Julie",12);
  6. Human f = new Child("Mona",6);
  7. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  8. hm.add(e);hm.add(f);

It is the argument that you need to insert. Of course before adding it do whatever checks are necessary.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 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: joining 2 database tables in a single query instead of arraying it and making a mess
Next Thread in Java Forum Timeline: Remove node not working in code





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


Follow us on Twitter


© 2011 DaniWeb® LLC