Movie Title MONTH DAY YEAR Director Actor1 Actor2 END Movie 2 Title MONTH DAY YEAR Director Actor 1 Actor 2 END
Corrected due date to be Apr. 1.
Corrected due date to be Mar. 31.
Corrected the 3rd step in the A level. It said to add “comparator” as a valid sort type, and has been corrected to say to add “rating” as a valid comparison type.
The goal of this assignment is to let you implement different sorting algorithms and compare them.
Questions can be directed to Shweta, Riea, Michael or Prof. Terveen.
|
Note
|
You have to finish all C level, B level, and A level parts in order to get an A, finish both C level and B level parts in order to get a B, and finish C level parts in order to get a C. |
|
Note
|
You must work in pairs on this assignment. Submit the assignment under the name of only one partner. |
To get started, download hw3.tgz and import the HW3 project from it into Eclipse (using the same procedure as in the labs).
You will be writing a program which reads in data about movies and sorts it. You will be able to observe the behavior of various sorting algorithms.
We provide a few classes and interfaces for you. The Sorter interface is to be implemented by your sorting algorithms (bubble sort and insertion sort). It defines one public method which takes an array to sort, a comparator to sort by, and a sort observer object for monitoring the sort progress.
The SortObserver interface is for monitoring sorting behavior. It provides methods which a sorting algorithm uses to notify it of various events. Sorting algorithms notify the sort observer when they start, stop, compare, and swap. See the SortObserver JavaDoc for details on this interface.
We provide two implementations of SortObserver. OpCounter counts the number of comparisons and swaps in a sort, and prints these numbers when the sort is completed. SortGUI displays the sort in a graphical interface. It will only display meaningful data when sorting by release date, but does let you watch the sort as it progresses.
The data file has the following format:
Movie Title MONTH DAY YEAR Director Actor1 Actor2 END Movie 2 Title MONTH DAY YEAR Director Actor 1 Actor 2 END
The first line of each record is the movie's title. The next line is the release date, in MONTH DAY YEAR format. After this comes the director, and then the actors. There can be any number of actors. After the last actor comes a line that says ``END, and then the next movie starts.
Programs frequently take arguments on their command line. If you are working in a shell, you specify them after the program's name. Java makes the arguments available as the args array in the main method. You can specify command line arguments for running a program in the “Program arguments” field on the “Arguments” tab in Eclipse's “Run” dialog.
Write a plain text file called README.txt that contains the names, IDs and UMN email addresses of you and your partner. It should also state which level your code is intended to meet. Also do the following for various levels mentioned below.
Create the class `Movie', and implement all methods and constructors required by its specification.
Write a comparator class which compares movies by release date. Your class should be called MovieReleaseDateComparator, and needs to implement the Comparator<Movie> interface.
For two release dates A and B, A < B if A comes before B in the calendar.
Write a class called BubbleSort which implements the Sorter interface using a bubble sort. Your sort must sort in non-decreasing order using the provided comparator. It must also post its progress to the sort observer, if the observer is not null. It needs to call the start method when it starts sorting, the done method when it is finished, and the compare and swap methods whenever it compares or swaps two values.
Your BubbleSort class must be generic.
Create a class called MovieSorter with a main method. The main method should do the following:
Create a new list to hold movies
Read movies from data file. The filename is provided as the first command line argument (args[0]). If the file does not exist, your program should print an error message to System.err and exit with a status of 1 by calling System.exit. If no file name is provided (args.length is 0), your program should print an error message and exit with a status of 2.
To do this, you will want to use java.util.Scanner. The Scanner object has a constructor which takes a File; a File can be constructed from a string containing a file name. Do not use Scanner's constructor that takes a string; it will not do what you want.
The Scanner class has some methods, hasNextLine and nextLine, which you can use to read lines from the file. To read the release date, you will want nextString (which reads a single “word”, or string that does not contain whitespace) and nextInt, which reads a number. Note that, after you have read those, you will need to call nextLine to get it to actually move to the next line before you can read the director.
Look at the ReleaseDate.Month JavaDoc, particularly the methods it exposes, to find things to use to convert string months into Month objects.
sorts the movie with bubble sort using the release date comparator and the OpCounter sort observer.
Iterate over the list of movie and display each one to System.out (using the display method). p
Run your program (the MovieSorter class) over the provided data file (provide its name as the first command line argument) and report the number of comparisons performed in README.txt.
Write a class called InsertionSort which implements Sorter by doing an insertion sort. It should sort in the same order as the bubble sort, and should use the sort observer in the same way. Again, it needs to be a generic class.
Write a comparator class which compares movies by director Your class should be called MovieDirectorComparator, and needs to implement the Comparator<Movie> interface.
Modify your main() method to handle command line options as well as a file name. The options, if any, are provided before the file name. You'll need to add code before you read the file to process these options. Your program must support the following options:
If this option is specified, the program uses the SortGUI rather than the OpCounter to observe the sort.
If the -sort option is specified, then the next argument is the name of the sort type. It will be “bubble” for a bubble sort and “insertion” for an insertion sort. The -sort option and the sort name are provided as separate arguments; if args[1] is “-sort”, then args[2] will be the name of the sort, and args[3] will be the next option or file name.
The -compare option specifies which comparator to use. It is one of “release-date”, “director”, or “title”.
The options can come in any order, and any or all of them can be specified. Your code should start at the beginning of the args array and walk through it. So long as it finds valid options, process them. As soon as it encounters something which is not a valid option, it should take that to be the filename.
For example, your program may be called like this:
java MovieSorter -gui -sort insertion movies.txt
In this case, it is to perform an insertion sort on the movies in movies.txt and display the sort using the GUI.
You also need to change the logic for determining if no file was specified to consider that no file is specified if all the arguments are options (the code processes the options and has no arguments left).
Run your program using the insertion sort and report the number of comparisons in README.txt.
Create an enum called MovieRating for representing the quality rating of a movie. It should have the values (in this order) WORTHLESS, POOR, AVERAGE, GOOD, and EXCELLENT.
Modify Movie.java and add rating as one of the data fields of the Movie class, with get and set methods (called getRating and setRating, respectively).
Write a comparator class called MovieRatingComparator which compares movies by rating. Add “rating” as a valid comparison type to your command-line argument processing.
Modify the test data file (testData1.txt) and add ratings as one of the parameters to test your code. Modify your main method to read the ratings. The rating of a movie should come right after the title.
Add a HeapSort implementation to your program, with the “heap” name for the command line argument.
To submit the assignment, use the package target of the provided Ant build file to create a package, and then submit this via the Submit system as “hw3”. Make sure it includes your README.txt file. If you do the extra credit, submit your extra credit solution as “hw3extra” (this must be in addition to a normal A-level submission as “hw3”).