This document provides some model answers to help supervisors.
Recall relational exercise 1c, in which you searched for pairs of actors who share the top billing of a movie. Let's implement the same thing using the document database.
As usual, the program should take the path to the database directory as the first command-line argument. The second command-line argument should be a number, namely the position to search for. If that number is x, we search for two actors who both have position x on the same movie.
You should be able to compile and run it as usual:
# On Windows javac -classpath document-db.jar Exercise3.java java -classpath .;document-db.jar Exercise3 document-small 1 # On Linux or Mac OS javac -classpath document-db.jar Exercise3.java java -classpath .:document-db.jar Exercise3 document-small 1
With the number 1 as position argument (as in the command line above) it should produce the following output:
Owen, Clive (I) and Yacuzzi, Juan Gabriel both have position 1 in Children of Men (2006)
If you change number 1 to 4, it should produce the following output:
Chevalier, Jon and Moore, Julianne both have position 4 in Children of Men (2006) Batey, Toya and Williams, Cobe both have position 4 in The Interrupters (2011)
The actor names should appear in alphabetical order.
Solution notes:
import uk.ac.cam.cl.databases.moviedb.MovieDB;
import uk.ac.cam.cl.databases.moviedb.model.*;
public class Exercise3 {
public static void main(String[] args) {
try (MovieDB database = MovieDB.open(args[0])) {
int requiredPosition = Integer.parseInt(args[1]);
// Scan over all movies
for (Movie movie : database.getByTitlePrefix("")) {
// Keeps track of the name of an actor with the required
// position in the current movie
String firstMatch = null, secondMatch = null;
for (CreditActor actor : movie.getActors()) {
// The null check is required because some actors have a
// null position, and you get a NullPointerException due to
// auto-unboxing if you just compare getPosition() == int
if (actor.getPosition() != null && actor.getPosition() == requiredPosition) {
if (firstMatch == null) {
firstMatch = actor.getName();
} else {
// We have found two actors with the required position.
// Now order their names lexicographically.
if (firstMatch.compareTo(actor.getName()) < 0) {
secondMatch = actor.getName();
} else {
secondMatch = firstMatch;
firstMatch = actor.getName();
}
System.out.println(firstMatch + " and " + secondMatch +
" both have position " + requiredPosition + " in " +
movie.getTitle());
}
}
}
}
}
}
}