15-110 FALL 2009 [CORTINA]

LAB 8

A movie consists of a title, rating and running time (in minutes). You will complete a class that maintains a collection of movies and performs various operations on the collection as described below.

EXERCISES

Click HERE to download a project named MovieDatabase that you will complete for this lab. Move this to your workspace and open it in Eclipse. This project contains the following classes:

  1. Read through the Movie class to see what public methods you can call from the MovieCollection class.

  2. In the MovieCollection class, complete the countMovies method. This method requires a parameter that stores a movie rating. You must complete the method so that it counts and returns the number of movies currently in the collection that match the rating given in the parameter.

    Once you are done writing this method, compile and run the CountTester class to check your answer. You should get the following final answer:

    The number of films with rating G:     3 
    The number of films with rating PG:    2 
    The number of films with rating PG-13: 4 
    The number of films with rating R:     1 
    The number of films with rating NC-17: 0 
    

  3. Complete the insert method so that it inserts each movie into the collection at the beginning, moving all movies currently in the collection over one position to make room for the new movie.

    In this version, there is private helper method doubleLength that doubles the size of the array. You should call this method only if the array is currently full when you try to insert a new movie. Thus, you can always add a new movie, and you don't need a warning that the array is full.

    Once you are done writing this method, compile and run the InsertTester class to check your answer. You should get the following final answer (make sure that the array size is correct too):

    Size of movieArray: 16 
    Number of movies in collection: 10 
    movieArray[0]: Borat R 104min. 
    movieArray[1]: Casino Royale PG-13 132min. 
    movieArray[2]: Ice Age: The Meltdown G 104min. 
    movieArray[3]: Happy Feet G 98min. 
    movieArray[4]: Superman Returns PG 120min. 
    movieArray[5]: The Da Vinci Code PG-13 145min. 
    movieArray[6]: X-Men: The Last Stand PG-13 128min. 
    movieArray[7]: Cars G 115min. 
    movieArray[8]: Night at the Museum PG 104min. 
    movieArray[9]: Pirates of the Carribbean II PG-13 132min. 
    

  4. In the MovieCollection class, complete the removeAll method. This method requires a parameter that stores a movie rating. You must complete the method so that it removes all movies in the current movie collection that match the rating given in the parameter. You do not have to change the size of the array.

    Once you are done writing this method, compile and run the RemoveAllTester class to check your answer.

    HINT: Examine each movie one at a time using a loop. If the movie matches the criteria, immediately shift all other movies over one position to remove that movie using another loop. (So you will have nested loops.) Be careful: if you shift movies, the next movie to check is in the same position as the movie you just removed!

HANDIN

At the end of lab, create a zip file of your program and submit it to the handin server: http://handin.intro.cs.cmu.edu/v1