Creating and Using Exceptions

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

Re: Creating and Using Exceptions

 
0
  #11
Nov 4th, 2008
look at the method below
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,706
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 228
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Creating and Using Exceptions

 
0
  #12
Nov 4th, 2008
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:
  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.
Check out my New Bike at my Public Profile at the "About Me" tab
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



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC