1 / 11

CS1020 Sit In Lab 2 Discussion

CS1020 Sit In Lab 2 Discussion. Movie Rating System. To understand the object oriented programming concept by building a “Movie Rating System” that manages the category and rating of movies. Functions needed BestMovie <category> WorstMovie <category> AverageRating <category> mostVote.

vidal
Download Presentation

CS1020 Sit In Lab 2 Discussion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1020Sit In Lab 2 Discussion

  2. Movie Rating System • To understand the object oriented programming concept by building a “Movie Rating System” that manages the category and rating of movies. • Functions needed • BestMovie <category> • WorstMovie <category> • AverageRating <category> • mostVote

  3. Examples • 2 // Number of CategoriesAction Comedy 3 // Number of MoviesBatman Action // <Movie name> <Category>TheDictatorComedySuperman Action7 // Number of Commandsvote Batman 3 vote Superman 2averageRating Action // output: 3averageRating Comedy // output: 0bestMovie Action // output: BatmanworstMovie Action // output: SupermanmostVote// output: Batman

  4. Breaking down the problem! • 1. Read and process the input • 2. What are the required data members in each classes? • List of movies in Category class? • Variables to store category name, totalVote and voters in Movie class? • 3. How to keep track of the average rating? • 4. Output result

  5. Step 1: Read & Process Input • Use Scanner object for input • Three parts to reading input • Read the number of categories (int) and use a loop to add them to our MovieSystem class • Likewise for adding all the movies//loopMovie m = new Movie(sc.next());movieSystem.addMovie(m);movieSystem.getCategoryFromName(sc.next()).addMovie(m); • Read the number of commands (int), and use a loop to read the specific operation ( String ) needed • e.g. String operation = sc.next(); if ( operation.equals( “vote” ) ) // do the necessary processing . . .

  6. Step 2: What are required data members for each class? MovieSystem - categories: List <Category> - movies: List <Movie> + addMovie(movie: Movie) + addVote(movie: String, rating: int) + bestMovie(category: String) : String + worstMovie(category: String) : String + avgRating(category: String) : double + mostVote() : String Movie - name: String - category: String - totalVote: int - voters: int + addVote(vote: int) + getAvgRating() : double Category - name: String - movies: List <Movie> + add(movie: Movie) + bestMovie() : String + worstMovie() : String + getAvgRating() : double • * the above listed does not the ALL the methods

  7. Movie Movie MovieSystem Movie Category Movie List of movies List of movies Movie Movie List of categories Category Movie Movie List of movies Movie Category List of movies

  8. Step 3a: Keeping track of Average Rating • Movie class (average rating of a movie)if (voters==0) return 0; //avoid division by 0return (double)totalVote / (double)voters; • Category class (average rating of a category)if (movies.size()==0) return 0; //avoid division by 0double total=0;for(int i=0; i<movies.size(); i++) total += movies.get(i).getAvgRating();return total / movies.size();

  9. Step 3b: Best & Worst Movie? Most Votes? • Movie with the mostVote can be solved in MovieSystemClass by iterating through all movies in the movie list and checking their amount of votes • Best & Worst Movie in a Category can be solved in Category class by iterating through all movies in that categoryfor (int i=0; i<movies.size(); i++) { if( bestMovie==null || movies.get(i).getAvgRating() > bestMovie.getAvgRating()) bestMovie = movie.get(i);}

  10. Step 4: Output result • For output, we will use our MovieSystem classe.g. to print the average rating of a category public double avgRating(String category) { return getCategory(category).getAvgRating(); } • Print what is returned by the above function System.out.printf("%.0f\n", movieSystem.avgRating(sc.next())); System.out.println(Math.round( movieSystem.avgRating(sc.next()))); Regarding printf, see: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

  11. That’s all!

More Related