943,697 Members | Top Members by Rank

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

Creating methods with Scenarios

Expand Post »
Hi, I have a Cinema class as shown in the code below. However I'm required to create my own exceptions and pass them to the controlling module (here method with scenarios). After an exception occurs the code should stop execution of current scenario and go to another one. The code should contains plenty of System.out.println statements so that the flow is clear.
Can someone give me a hand on this please, I'm a newbie in Java and I don't know how to go about this creating my own exceptions.

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3. public class Cinema
  4. {
  5. float cost;
  6. String Title;
  7.  
  8. String mrPG="PG";
  9. String mrA="A";
  10.  
  11. Movie movieNew = new Movie();
  12. //MovieRating class
  13. MovieRating mrating = new MovieRating();
  14. private ArrayList<Human> hm = new ArrayList();
  15.  
  16. public Cinema()
  17. {
  18.  
  19. }
  20. public Cinema (String mtitle, float pounds)
  21. {
  22. this.Title=mtitle;
  23. this.cost = pounds;
  24. }
  25.  
  26. public float getcost (int people)
  27. {
  28. return cost * people;
  29. }
  30.  
  31. /**
  32.   * Method check that the movie title and rating have been set,
  33.   * if not throw an exception.
  34.   */
  35. public void setMovie(Movie movieNew) throws IOException
  36. {
  37. try
  38. {
  39. if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
  40. {
  41. System.out.println("The movie is called " +movieNew.getTitle()+
  42. " and is rated " +movieNew.getRating()+ ".");
  43. }
  44. else
  45. {
  46. System.out.println("Movie title and rating not set");
  47. }
  48. }
  49. catch(Exception e)
  50. {
  51. System.out.println("Movie properties not set.");
  52. }
  53. }
  54. /*
  55.   * this method first checks if the movie properties are set.
  56.   */
  57. public void showMovie() throws IOException
  58. {
  59. try
  60. {
  61. if(movieNew.getTitle()!=null)
  62. {
  63. System.out.println(movieNew.getTitle()+" Now Showing"); }
  64. else
  65. {
  66. System.out.println("Show has no Movie title.");
  67. }
  68. }
  69. catch(Exception e)
  70. {
  71. System.out.println("Check the movie properties.");
  72. }
  73. finally
  74. {
  75. System.out.println("Thanks for coming.");
  76. }
  77. }
  78.  
  79. public void addMovieGoers(Human h)
  80. {
  81. Human a = new Adult("Joe",43);
  82. Human b = new Adult("Sue",39);
  83. Human c = new Adult("Tracy",20);
  84. Human d = new Child("Sammy",17);
  85. Human e = new Child("Julie",12);
  86. Human f = new Child("Mona",6);
  87. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  88. hm.add(e);hm.add(f);
  89.  
  90. hm.trimToSize();
  91. String toPrint = "";
  92. try
  93. {
  94. if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
  95. {
  96. if(mrPG.equals(movieNew.getRating())) //movies rated PG
  97. {
  98. System.out.println("The following can only watch under parental guidance:");
  99. for(int i = 0; i < hm.size(); i++)
  100. {
  101. Human test = (Human)hm.get(i);
  102. if ( test instanceof Child)
  103. {
  104. if(toPrint.equals(""))
  105. {
  106. System.out.println(test);
  107. }
  108. else
  109. {
  110. System.out.println("Children: " + toPrint);
  111. }
  112. }
  113. }
  114. }
  115. else if(mrA.equals(movieNew.getRating())) //movies rated Adult
  116. {
  117. System.out.println("This movie is rated A");
  118. for ( int i = 0; i < hm.size(); i++)
  119. {
  120. Human test = (Human)hm.get(i);
  121. if ( test instanceof Adult)
  122. {
  123. if(toPrint.equals(""))
  124. {
  125. System.out.println(test);
  126. }
  127. else
  128. {
  129. System.out.println("Adults: " + toPrint);
  130. }
  131. }
  132. }
  133. }
  134. else //Movies rated G
  135. {
  136. System.out.println("This is movie rated G. So all can watch"+
  137. ": ");
  138. for ( int i = 0; i < hm.size(); i++)
  139. {
  140. System.out.println(i);
  141. }
  142. }
  143. }
  144. }
  145. catch(Exception x)
  146. {
  147. System.out.println("Movie properties are missing. Please set the Title"+
  148. " and rating first.");
  149. }
  150.  
  151. }
  152.  
  153. public static void main(String args[]) throws IOException
  154. {
  155. Cinema cn = new Cinema();
  156. Human h = new Human();
  157. //instantiating and setting properties to the movie
  158. Movie movieNew = new Movie();
  159. movieNew.setName("Comedy");
  160. movieNew.setRating("PG");
  161. movieNew.setTitle("Mr Bin");
  162.  
  163. System.out.println("Setting Movie properties:");
  164. cn.setMovie(movieNew);
  165.  
  166. System.out.println("Movie Goers are: ");
  167. cn.addMovieGoers(h);
  168.  
  169. }
  170. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 27th, 2008
0

Re: Creating methods with Scenarios

Woah!! Woah!! Woah!! First things first, don't catch Exception unless you really need to! Exception will catch *all* exceptions. I'd rather create my own exception class which inherits the base class. Then you can catch that one!
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Oct 27th, 2008
0

Re: Creating methods with Scenarios

okay, I've created my exception class as:

Java Syntax (Toggle Plain Text)
  1. public class MyException extends Exception
  2. {
  3. String exception;
  4. public MyException()
  5. {
  6. super();
  7. exception="Unknown";
  8. }
  9. public MyException(String exp)
  10. {
  11. super(exp);
  12. this.exception=exp;
  13. }
  14. public String getException()
  15. {
  16. return this.exception;
  17. }
  18. }

and changed my method to look like this:
Java Syntax (Toggle Plain Text)
  1. public void setMovie(Movie movieNew) throws MyException
  2. {
  3. if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
  4. {
  5. System.out.println("The movie is called " +movieNew.getTitle()+
  6. " and is rated " +movieNew.getRating()+ ".");
  7. }
  8. else
  9. {
  10. throw new MyException("The Movie properties not set");
  11. }
  12. }

So my question is, is this how to create my own exceptions and passing them to the controlling module (here method with scenarios)? and, After an exception occurs does the code stop execution of current scenario and go to another one. or what else should I include.

Another requirement to my Cinema class is to add another static method to the cinema class that demonstrates the usage of the cinema class. Include multiple scenarios to show how the exceptions work.

the main method is:
Java Syntax (Toggle Plain Text)
  1. public static void main(String args[]) throws MyException
  2. {
  3. Cinema cn = new Cinema();
  4. Human h = new Human();
  5. //instantiating and setting properties to the movie
  6. Movie movieNew = new Movie();
  7. movieNew.setName("Comedy");
  8. movieNew.setRating("G");
  9. movieNew.setTitle("Mr Bin");
  10.  
  11. System.out.println("Setting Movie properties:");
  12. cn.setMovie(movieNew);
  13.  
  14. System.out.println("Current Show:");
  15. cn.showMovie();
  16.  
  17. System.out.println("Movie Goers are: ");
  18. cn.addMovieGoers(h);
  19.  
  20. }

Finally, how do I add code to empty the Cinema whether the movie was shown or not. (In the showMovie() method below)
Java Syntax (Toggle Plain Text)
  1. public void showMovie() throws MyException
  2. {
  3. movieNew.setName("Comedy");
  4. movieNew.setRating("G");
  5. movieNew.setTitle("Mr Bin");
  6. if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
  7. {
  8. System.out.println(movieNew.getTitle()+" Now Showing");
  9. }
  10. else
  11. {
  12. throw new MyException ("The movie properties are not set.");
  13. }
  14. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 27th, 2008
0

Re: Creating methods with Scenarios

Click to Expand / Collapse  Quote originally posted by Achupa ...
So my question is, is this how to create my own exceptions and passing them to the controlling module (here method with scenarios)? and, After an exception occurs does the code stop execution of current scenario and go to another one. or what else should I include.
You also can use try catch to use your exceptions. See following example:
Java Syntax (Toggle Plain Text)
  1. public int SomMethod() {
  2. try {
  3. //Your codes
  4. }
  5. catch (MyException e) {
  6. //do whatever you wanted to do.
  7. }
  8. }

Click to Expand / Collapse  Quote originally posted by Achupa ...
Another requirement to my Cinema class is to add another static method to the cinema class that demonstrates the usage of the cinema class. Include multiple scenarios to show how the exceptions work.
You can call a static method by:
Java Syntax (Toggle Plain Text)
  1. public static returnType methodName (argument)
You don't have to create any object in order to call this method. You can call the method any time you want- from static method. So, you will basically call it from your main function (as that is by default static). Here are two examples of calling a static method:
Java Syntax (Toggle Plain Text)
  1. public static void main(String args[]) throws MyException
  2. {
  3. //direct call to a static method:
  4. yourStaticMethod(arg);
  5. //Through class
  6. Cinema.yourStaticMethod(arg); //NOTE: Cinema is the name of the class, not the object.
  7.  
  8. }


Click to Expand / Collapse  Quote originally posted by Achupa ...
Finally, how do I add code to empty the Cinema whether the movie was shown or not. (In the showMovie() method below)
Your question is not clear to me. You may use another property called showStatus which will contain the information about the show or a property called showTime to contain the time, which will compare time with current time and display if the show is available or not. Is that what you wanted?
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 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: Arrays in Family class
Next Thread in Java Forum Timeline: Execeptions





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


Follow us on Twitter


© 2011 DaniWeb® LLC