| | |
Cinema
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 24
Reputation:
Solved Threads: 0
Hi, I had posted this question before and I did appreciate the response I got. However I still need some little help as:
1.In the addMovieGoers method check that every member of the collection is a human and that they are old enough to watch the movie, if not throw an exception. Note that although the family has getMovieGoers method, you can't be sure it was used to build the collection.
2.Finally 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 did not include multiple scenarios
How Do I achieve the two conditions above?
the code is as:
//Cinema class
1.In the addMovieGoers method check that every member of the collection is a human and that they are old enough to watch the movie, if not throw an exception. Note that although the family has getMovieGoers method, you can't be sure it was used to build the collection.
2.Finally 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 did not include multiple scenarios
How Do I achieve the two conditions above?
the code is as:
//Cinema class
Java Syntax (Toggle Plain Text)
import java.util.*; public class Cinema { float cost; String Title; //Movie class private static Movie movieNew = new Movie(); private static ArrayList<Human> hm = new ArrayList(); public Cinema() { } public Cinema (String mtitle, float pounds) { this.Title=mtitle; this.cost = pounds; } public float getcost (int people) { return cost * people; } /** * Method check that the movie title and rating have been set, * if not throw MoviePropertiesNotSetException. */ public void setMovie(Movie movieNew) throws MoviePropertiesNotSetException { if(movieNew.getTitle()!=null && movieNew.getRating()!=null) { System.out.println("The movie is called " +movieNew.getTitle()+ " and is rated " +movieNew.getRating()+ "."); } else { throw new MoviePropertiesNotSetException("The Movie properties not set"); } } /** * Method check that the movie title and rating have been set, * if not throw MoviePropertiesNotSetException. */ public void showMovie() throws MoviePropertiesNotSetException { if ((movieNew!=null) && (movieNew.getTitle()!=null) && (movieNew.getRating()!=null)) { System.out.println("Showing... "+movieNew.getTitle()); } else { throw new MoviePropertiesNotSetException ("The movie properties are not set."); } } /**Checks if Movie properties are set, if not throws MyException * takes a collection (ArrayList) of Human - Adult and Child * and checks whether they're old enough to watch a movie using * the canWatchMovie() method in Adult and Child class. */ public void addMovieGoers(ArrayList<Human> hm) throws MoviePropertiesNotSetException,IllegalAgeException { if(movieNew.getRating()!=null && movieNew.getTitle()!=null) { String mRt = movieNew.getRating(); /** * Movie rated PG */ if(movieNew.getRating().equalsIgnoreCase("pg")) { System.out.println("Movie Goers are::"); for(int i = 0; i < hm.size(); i++) { Human test = (Human)hm.get(i); if ( test.canWatchMovie(mRt)) { System.out.println(test); } else { throw new IllegalAgeException("The rest are of Illegal Age!"); } } } /** * Movie rated A */ else if(movieNew.getRating().equalsIgnoreCase("a")) { System.out.println("Movie goers are: "); for ( int i = 0; i < hm.size(); i++) { Human test = (Human)hm.get(i); if ( test.canWatchMovie(mRt)) { System.out.println(test); } else { throw new IllegalAgeException("The rest are of Illegal Age!"); } } } /** * Movie rated G */ else if(movieNew.getRating().equalsIgnoreCase("g")) { System.out.println("Movie goers are: "); for ( int i = 0; i < hm.size(); i++) { Human test = (Human)hm.get(i); if ( test.canWatchMovie(mRt)) { System.out.println(test); } else { System.out.println("The rest are below required age."); } } } /** * Throw Exception should the movie properties be missing */ else { throw new MoviePropertiesNotSetException("The movie properties are not set"); } } } public static void main(String args[]) throws MoviePropertiesNotSetException, IllegalAgeException { Cinema cn = new Cinema(); try { Human a = new Adult("Joe",43); Human b = new Adult("Sue",39); Human c = new Adult("Tracy",20); Human d = new Child("Sammy",17); Human e = new Child("Julie",12); Human f = new Child("Mona",4); hm.add(a);hm.add(b);hm.add(c);hm.add(d); hm.add(e);hm.add(f); hm.trimToSize(); //setting properties to the movie movieNew.setName("Comedy"); movieNew.setRating("G"); movieNew.setTitle("Mr Bin"); cn.setMovie(movieNew); cn.showMovie(); cn.addMovieGoers(hm); } catch(MoviePropertiesNotSetException exp) { System.out.println(exp.getMessage()); } catch(IllegalAgeException msg) { System.out.println(msg.getMessage()); } finally { System.out.println("Movie is over! Have a good day."); movieNew=null; cn=null; } } }
Regarding requirement 1, not too sure exactly what the issue is, and I am not too sure what the family class looks like. Your conditions are also redundant (you are checking the same condition for each case, so you don't really need to divide these out).
Regarding the second requirement, it seems you already have 1 scenario, so just add more.
Hope that helps
Regarding the second requirement, it seems you already have 1 scenario, so just add more.
Hope that helps
Well yes you would probably end up needing another try / catch block. Keep in mind though, a scenario is like a story.
To test some other cases, perhaps have a movie where ALL the humans watch the movie, and one where NONE watch the movie.
Does that make sense?
To test some other cases, perhaps have a movie where ALL the humans watch the movie, and one where NONE watch the movie.
Does that make sense?
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 1
•
•
•
•
To test some other cases, perhaps have a movie where ALL the humans watch the movie, and one where NONE watch the movie.
Java Syntax (Toggle Plain Text)
public static void main(String args[]) throws MoviePropertiesNotSetException, IllegalAgeException { Cinema cn = new Cinema(); Human a = new Adult("Joe",43); Human b = new Adult("Sue",39); Human c = new Adult("Tracy",20); Human d = new Child("Sammy",17); Human e = new Child("Julie",12); Human f = new Child("Mona",4); hm.add(a);hm.add(b);hm.add(c);hm.add(d); hm.add(e);hm.add(f); hm.trimToSize(); //scenario 1: Movie rated G try { //setting properties to the movie movieNew.setName("Comedy"); movieNew.setRating("G"); movieNew.setTitle("Mr Bin"); cn.setMovie(movieNew); cn.showMovie(); cn.addMovieGoers(hm); } catch(MoviePropertiesNotSetException exp) { System.out.println(exp.getMessage()); } catch(IllegalAgeException msg) { System.out.println(msg.getMessage()); } //Scenario 2: Movie rated PG try { //setting properties to the movie movieNew.setName("Action"); movieNew.setRating("PG"); movieNew.setTitle("The Fugitive"); cn.setMovie(movieNew); cn.showMovie(); cn.addMovieGoers(hm); } catch(MoviePropertiesNotSetException exp) { System.out.println(exp.getMessage()); } catch(IllegalAgeException msg) { System.out.println("Exception "+msg.getMessage()); } //Scenario 3: Movie Rated A try { //setting properties to the movie movieNew.setName("Action"); movieNew.setRating("A"); movieNew.setTitle("Lost in The Woods"); cn.setMovie(movieNew); cn.showMovie(); cn.addMovieGoers(hm); } catch(MoviePropertiesNotSetException exp) { System.out.println("Movie properties missing: "+exp.getMessage()); } catch(IllegalAgeException msg) { System.out.println("Exception "+msg.getMessage()); } finally { System.out.println("Movie is over! Have a good day."); movieNew=null; cn=null; } }
![]() |
Similar Threads
- G5 - Cinema Display not working - help please (Apple Hardware)
- Is the Apple 30 inch Cinema Display worth it (IT Professionals' Lounge)
- 30" cinema display? (Mac Rumors and Reports)
- Dani... tell everyone what you baught.. (Apple Hardware)
- Welcome PC Mod Kingdom peeps! (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: anyone help wth my anagram game!!!
- Next Thread: Methods
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component css csv database eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oriented output panel phone print printf problem program programming project projects recursion replaydirector reporting researchinmotion robot rotatetext scanner screen se server service set size sms software sort sql string swing test threads transfer tree ubuntu windows





