Homework 14


Assignment: 1 problem, 100 points Reading: Your CS5 lecture notes and the online readings for week 14.


Problem 1     Movie Database     (100 points)     [topics: classes, recursive data structures]

In this problem you will implement a film information system inspired by the Internet Movie Database http://www.imdb.com. Your program will allow a user to create a database interactively---by entering films one by one at the keyboard---or in "batch" mode---by reading a bunch of films in from a file. It will also allow the user to retrieve information along different dimensions. For instance, list all the films in the database from the year 2000. Or, list all of the films in the database with "Rings" in the title.

A film's definition has multiple aspects: title, director, year, etc. To handle this aggregate information, you'll implement a Film class as specified below. Similarly, you'll create a Director class to handle various data stored for a given director. You'll implement a FilmDB class to manage the collection of films you've added to your database. Similarly, you'll implement a DirectorDB class to manage the collection of directors that have been (implicitly) added to your database (each time a film is entered with a new director, the director database incorporates a new Director object).

One interesting, fun challenge in this problem is the intertwined nature of the classes you'll be defining. For instance, in addition to a director's first and last name, a Director object contains a film database, FilmDB, whose sole purpose is to make it easier to identify films on a per-director basis. This feature leads to circular data in that FilmDB contains Film objects, each which contains a Director object, which in turn contains a FilmDB. These classes and their relationships to one another are displayed in the following figure:

class structure
Although this figure may look complicated, it requires no special work on your part. Simply define each class as you always have, specifying variable and methods names and types as needed. Have fun with this!

In general, you'd like your database to be able to adapt in size as more information is added (this is more efficient because you only allocate more resources when they are needed). However, to simplify the problem, you're allowed to assume each database you create has a fixed (suitably large, e.g. 100) size capacity. What this means is that, underneath the hood, your FilmDB and DirectorDB classes can simply create appropriate arrays of this length in their constructors. Should you then wish add more objects than this capacity supports, your program will inform the user they can't do this. In this scenario, it is important to keep some extra state around---for an array can tell you how long it is, but it doesn't know how many of its objects are currently used. For this reason, your database classes will also maintain a count field. Moreover, before any array element is assigned to a specific object instance, it will contain Java's all-purpose null value.

Extra Credit: If you wish to do extra credit on this problem, you could create your database arrays in such a way that they resize themselves as needed (they could thus start out with a size of 1 as opposed to 100!). Although this is a bit more involved because of the intertwined nature of the databases, it is definitely doable! If you'd like to try and implement this feature, only do so after you've already gotten the required version working robustly. Then you could extend that code (keep a copy of your original in case you don't get this working). It turns out a very efficient way to grow an array is to always double its size (and then copy the old items into the larger array). If you take this approach, you might consider adding a resize method to each of your database classes. Another viable option would be to use existing Java libraries (e.g. a grow-able array).

Submission: Place each of these classes in the CS5App.java file and submit the CS5App.java file under Homework 14, Problem 1. If you do the extra credit, submit that as Homework 14, Problem 2.

What's next: The rest of this document details the functionality of the classes you should build, followed by some documentation regarding some string methods you'll find useful and a sample run.


The Film Class

Required data members: Required constructor: Required methods: Additional recommendations:

The Director Class

Required data members: Required constructor: Required other methods:

The FilmDB Class

Required data members:

Required constructors: Required other methods:

The DirectorDB Class

Required data members: Required constructors: Required methods that are similar in form and function to those in the FilmDB class: Required other methods:

The CS5App Class

Data Members: Methods:

Helpful String Methods

Java provides String methods that will be useful to you in this assignment:

Below are some examples of how these methods can be used given variables String s = "Hamburger" and String t = "Urge".

A Sample Run

Here is a sample run that displays how your code should function. This section only contains a small subset of the possible things we might try when testing your code. Be sure to test your code carefully before submitting it!

If you'd like to use the files I've used for testing (twofilms.txt and init.txt) these can be downloaded from http://www.cs.hmc.edu/~bthom. Another more extensive database file, which can be used for more exhaustive testing, as also available there (hardcore.txt).


Welcome to CS5's movie database!

There are 0 films in our database (max: 100)

Please choose an option from the following list:
   0 Display all directors
   1 Display all films
   2 Display all films by Title
   3 Display films by Director
   4 Display films by Year
   5 Display films by Rating (G, PG, ...)
   6 Display films by Review (0-10)
   7 Add a new film
   8 Save database to file
   9 Read database from file
  42 Quit
Which option would you like? 9

Enter a file name: twofilms
Loading database contents from file ../twofilms.txt
100
2
Enter the title:                  Lord of the Rings: Return of
Enter the director's last name:   Jackson
Enter the director's first name:  Peter
Enter the movie's year:           2003
Enter the movie's MPAA rating:    PG-13
Enter a review score:             9.0

Enter the title:                  Gigli
Enter the director's last name:   Brest
Enter the director's first name:  Martin
Enter the movie's year:           2003
Enter the movie's MPAA rating:    R
Enter a review score:             2.2

There are 2 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 0

Director #0 is Brest, Martin
Director #1 is Jackson, Peter

There are 2 films in our database (max: 100)
< menu snipped to save trees >
Which would you like? 7

Enter the title:                  King Kong
Enter the director's last name:   jackson
Enter the director's first name:  peter
Enter the movie's year:           2005
Enter the movie's MPAA rating:    x
That is not a valid rating. Enter another: PG-13
Enter a review score:             6.0

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 1

Title:    Gigli
Director: Brest, Martin
Year:     2003
Rating:   R
Review:   2.2

Title:    King Kong
Director: Jackson, Peter
Year:     2005
Rating:   PG-13
Review:   6.0

Title:    Lord of the Rings: Return of the King
Director: Jackson, Peter
Year:     2003
Rating:   PG-13
Review:   9.0

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 2

Enter a titlepiece: king

Title:    King Kong
Director: Jackson, Peter
Year:     2005
Rating:   PG-13
Review:   6.0

Title:    Lord of the Rings: Return of the King
Director: Jackson, Peter
Year:     2003
Rating:   PG-13
Review:   9.0

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 3

Enter last name:  Peter
Enter first name: Jackson

No films by director "Peter,Jackson"

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 3

Enter last name:  bREst
Enter first name: MaRtIn

Title:    Gigli
Director: Brest, Martin
Year:     2003
Rating:   R
Review:   2.2

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 8

Enter a file name: threefilms
Saving the database's contents to the file ../threefilms.txt

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 5

Enter MPAA rating:  santa
That is not a valid rating. Enter another: r

Title:    Gigli
Director: Brest, Martin
Year:     2003
Rating:   R
Review:   2.2

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 9

Enter a file name: init

Loading database contents from file ../init.txt

There are 0 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 1

No films in database to display

There are 0 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 9

Enter a file name: threefilms

Loading database contents from file ../threefilms.txt

Enter title:                      Gigli
Enter director's last name:       Brest
Enter the director's first name:  Martin
Enter the movie's year:           2003
Enter the movie's MPAA rating:    R
Enter a review score:             2.2
Enter title:                      King Kong
Enter director's last name:       Jackson
Enter the director's first name:  Peter
Enter the movie's year:           2005
Enter the movie's MPAA rating:    PG-13
Enter a review score:             6.0
Enter title:                      Lord of the Rings: Return of the King
Enter director's last name:       Jackson
Enter the director's first name:  Peter
Enter the movie's year:           2003
Enter the movie's MPAA rating:    PG-13
Enter a review score:             9.0

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 6

Enter a review: 8

Title:    Lord of the Rings: Return of the King
Director: Jackson, Peter
Year:     2003
Rating:   PG-13
Review:   9.0

There are 3 films in our database (max: 100)
< menu snipped to save trees >
Which option would you like? 42

Until next time, your movie is up!