This presentation is the property of its rightful owner.
1 / 11

# CS1020 Sit In Lab 2 Discussion PowerPoint PPT Presentation

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.

CS1020 Sit In Lab 2 Discussion

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## CS1020Sit 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

### Examples

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

### 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

### 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

• 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 . . .

### Step 2: What are required data members for each class?

MovieSystem

- categories: List <Category>

- movies: List <Movie>

+ bestMovie(category: String) : String

+ worstMovie(category: String) : String

+ avgRating(category: String) : double

+ mostVote() : String

Movie

- name: String

- category: String

- totalVote: int

- voters: int

+ getAvgRating() : double

Category

- name: String

- movies: List <Movie>

+ bestMovie() : String

+ worstMovie() : String

+ getAvgRating() : double

• * the above listed does not the ALL the methods

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

### 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();

### 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);}

### 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