Object Oriented Review

Objectives
  • Analyze and filter real world data.

  • Create multiple constructors.

  • Learn about method and constructor overloading.

  • Explore the toString and equals method.

Getting Started

Create a new Java project called W3L2 and import W3L2.jar.
Your directory should look like this:

W3L2/
├── resources
│   ├── movie_actors.dat
│   ├── movie_countries.dat
│   ├── movie_directors.dat
│   ├── movie_genres.dat
│   ├── movie_locations.dat
│   ├── movie_ratings.dat
│   ├── movie_keywords.dat
│   ├── movie_titles.dat
│   ├── movie_years.dat
│   ├── keywords.dat
│   ├── user_ratings.dat
│   ├── user_keywords.dat
│   └── readme.txt
└── src
    ├── Movie.java
    ├── MovieLibrary.java
    ├── Rating.java
    └── KeyWord.java
Data

The data for this assignment was collected and reformatted from the following website. If you are interested, the provided readme has a description of the data formats and some additional information.

Description

You will be creating a MovieLibrary to store information from over 10,000 movies. Each Movie instance will store basic information, RottenTomato ratings, and list of keywords that describe the movie. Once created, you will create methods to filter given specific conditions and format the output.

The KeyWord Class

This class consists of a descriptive word and the frequency that word appears.

Java specifies that member/instance variables are called fields.
Declare the following fields:

  • A private String to store the keyword

  • A private int to store the frequency

Use the javadoc to define and implement the rest of the methods. Once you are finished, use the following code to test your class.

System.out.println("The KeyWord class:");
KeyWord one = new KeyWord("magical", 20);
KeyWord two = new KeyWord("waste of time", 3);

System.out.printf("label: %s, frequency: %d\n", one.getLabel(), two.getFrequency());
System.out.println(one);
System.out.println(two);
The Rating Class

This is a class where ratings from RottenTomato are stored.

Declare the following fields:

  • A private int to store the average critic score

  • A private int to store the number of critics in the critic score

  • A private int to store the average audience score

  • A private int to store the number of people included in the audience score

Use the javadoc to declare and implement the rest of the methods.

Tip
Declare and implement the four argument constructor first. Explicitly invoke this constructor when implementing the noargs constructor by using the this keyword.

Once you are finished, use the following code to test your class.

System.out.println("The Rating class:");
Rating noargs = new Rating();
Rating rating1 = new Rating(30, 10, 45, 100);
System.out.printf("Critic Score: %d\nNumber of Critics: %d\nAudience Score: %d\nNumber of people: %d\n",
                   rating1.getCriticScore(), rating1.getNumCritics(), rating1.getAudienceScore(), rating1.getNumCritics());
System.out.println(noargs);
System.out.println(rating1);
The Movie Class

This class creates a Movie object. A Movie object requires a title, year, and list of actors. A Movie object can also have a list of genres, a Rating, and a list of KeyWords.

Declare the following fields:

  • A private final String to store the title of the movie

  • A private final int to store the year

  • A private final List<String> to store the actors

  • A private List<String> to store the genres

  • A private Rating to store the movie rating

  • A private List<KeyWord> to store keywords used to describe the movie

You will construct an equals method for this class. Read these additional notes before implementation.

Use the javadoc to declare and implement the rest of the methods. Once you are finished use the following code to test your class.

Movie movie1 = new Movie("Spirited Away", 2001,
                         Arrays.asList("Rumi Hiiragi", "Miyu Irino", "Mari Natsuki",
                                       "Takashi Naito", "Yasuko Sawaguchi", "Tatsuya Gashuin"));
Movie movie2 = new Movie("Spirited Away", 2001,
                         Arrays.asList("Rumi Hiiragi", "Miyu Irino", "Yasuko Sawaguchi",
                                       "Takashi Naito", "Mari Natsuki", "Tatsuya Gashuin"),
                         Arrays.asList("Animation", "Adventure", "Family"),
                         new Rating(97, 180, 96, 334759),
                         Arrays.asList(new KeyWord("spirit", 5),
                                       new KeyWord("f rated", 20),
                                       new KeyWord("female protagonist", 32),
                                       new KeyWord("surprise ending", 10)) );

Movie movie3 = new Movie("Coco", 2017,
                         Arrays.asList("Alanna Ubach", "Benjamin Bratt", "Edward James Olmos",
                                       "Gael Garcia Bernal", "Cheech Marin", "Gabriel Iglesias"),
                         Arrays.asList("Animation", "Adventure", "Comedy"),
                         new Rating(),
                         Arrays.asList(new KeyWord("afterlifet", 1),
                                       new KeyWord("disney", 1)));


System.out.printf("Testing the Getters:\n" +
                          "movie2.getTitle() %s\n" +
                          "movie2.getYear() %d\n" +
                          "movie2.getActors() %s\n" +
                          "movie2.getGenres() %s\n" +
                          "movie2.getRating() %s\n" +
                          "movie2.getKeyWords() %s\n",
                  movie2.getTitle(), movie2.getYear(), movie2.getActors(),
                  movie2.getGenres(), movie2.getRating(), movie2.getKeyWords());
System.out.println("\nTesting the toString method:\n" + movie1);
System.out.printf("movie1 and movie2 should be equal (true) -> %b\n", movie1.equals(movie2));
System.out.printf("movie1 and movie3 should be not be equal (false) -> %b\n\n", movie1.equals(movie3));

System.out.println("hashCode should always be changed along with the equals method.");
System.out.println(movie1.hashCode());
System.out.println(movie2.hashCode());
System.out.println(movie3.hashCode() + "\n");

try {
    movie2.actors.add("Yumi Tamai");
    System.err.println("FAILURE. Check to make sure that actors is an unmodifiable list.");
}catch(UnsupportedOperationException e){
    System.out.println("SUCCESS. Used an unmodifiable list for actors.");
}
The MovieLibrary Class

This is where the Movie objects are stored.

Declare the following fields:

  • a List<Movie> of movies

Use the javadoc to declare and implement the rest of the methods.

Once you have finished implementing the code, download the FileParser class.

In the main method of your MovieLibrary class:

  • Create an instance of FileParser.

  • Create an instance MovieLibrary using the return value of the getAllMovies in the FileParser class. Use the following file names for the arguments:

"resources/movie_titles.dat"
"resources/movie_years.dat"
"resources/movie_actors.dat"
"resources/movie_genres.dat"
"resources/keywords.dat"
"resources/movie_keywords.dat"
"resources/movie_ratings.dat"
  • Copy and paste the following questions into the main method of your MovieLibrary class.
    Answer each question and supplement the answer with the code you used.

System.out.println("The MovieLibrary class:");
// 1. How many movies are there in total?

// 2. How many movies did the audience rate 95% or above?

// 3. How many movies did the critics rate 0% to 5%?

// 4. Some movies don't have KeyWords. Explain how you could change
//   the getDescription method to only print movies that have keywords.

// 5. How would you sort the KeyWords by frequency? What class/classes would you modify?
// How would you deal with keywords that have the same frequency?

Important
Once you have completed the code and answered the questions, show your code to the TA or helper.