- COMP.CS.140
- 8. Generics
- 8.6 ⌛⌛ Movie data as a stream #2
⌛⌛ Movie data as a stream #2¶
Place your code into files named Movie.java and MovieAnalytics2.java in the round8/streams2
directory of your local repository. The test material is available in the corresponding directory
in the remote materials repository.
This task concerns using the Java streams with tools from the Collectors
class. The goal is to
implement the MovieAnalytics2
class for producing grouped information about movie data by
using the groupingBy
function of the Collectors
class. This task uses the Movie
class
from the previous task to represent movie data.
The MovieAnalytics2
class should have the following public features:
Constructor
MovieAnalytics2()
that initializes an empty movie database. AMovie
list is a sufficient data structure also here.Member function
void populateWithData(String fileName)
that reads movie data from the file specifed by the parameter. The function behaves as the corresponding function in previous task, but it must be implemented differently (see below).Member function
void printCountByDirector(int n)
that prints out then
directors that have directed the most movies, in descending order of the number of movies. Directors with equally many movies are ordered by their names using the default order of theString
objects. Each director is printed in the form"director: x movies"
, wherex
is the number of movies directed by that particular director. The output is printed in a single line that ends to a new line character.Member function
void printAverageDurationByGenre()
that prints out all movie genres of the database in ascending order of the average duration of movies in that genre. Movies in the same genre are ordered by title using the default order of theString
objects. Each genre is printed in the form"genre: average"
, whereaverage
is the average duration of movies in that genre, using two decimals of precision. The output is printed in a single line that ends to a new line character.Member function
void printAverageScoreByGenre()
that prints out all movie genres of the database in descending order of the average rating scores of movies in that genre. Genres with the same average rating are ordered by genre name using the default order of theString
objects. Each genre is printed in the form"genre: average"
, whereaverage
is the average rating score of movies in that genre, using two decimals of precision. The output is printed in a single line that ends to a new line character.
You should use streams in this task. To this end, the grading system seeks to enforce a rule that
the file MovieAnalytics2.java
cannot contain any for
or while
loops. Since this restriction applies
also to the reading of the file contents, implement also that part using a stream. Use the lines()
function
of the BufferedReader
class to create a stream that reads input line by line.
A note about the grader’s so-called “loop detection”
The grader uses a very crude method for detecting loops: It checks that the source code does not
contain the keywords “for” or “while”. The detection is overly eager in the sense that it matches
also words like “form” that begin with the forbidden word “for”. These keywords are allowed, however,
if they are directly preced by a dot, for example .forEach
and .format
are allowed.
Therefore, do not use variable or function names or even words in comments that begin with
“for” or “while”.
Testing¶
You may test your implementation by using the test program given in the file MovieTest2.java
,
the movie data files input1.txt
, input1.txt
and input1.txt
and the example output given
in the files output1.txt
, output2.txt
and output3.txt
. Place these files to the same
directory with your code, compile the test program, for example, as javac *.java
and run
the tests as java MovieTest2 input1.txt 10
, java MovieTest2 input2.txt 15
and
java MovieTest2 input3.txt 20
. The expected outputs of these tests are given in the files
output1.txt
, output2.txt
and output3.txt
.
A+ presents the exercise submission form here.