- COMP.CS.140
- 7. Generics
- 7.6 ⌛⌛ Movie data vs stream #2
⌛⌛ Movie data vs stream #2¶
Place your code into files named Movie.java and MovieAnalytics2.java in the directory
Round7/streams2. Also remember to pull task material from student_template_project
.
This task concerns using java streams with tools from the Collectors
class. The goal is to
implement a class MovieAnalytics2
that produces grouped information about movie data by
using the groupingBy
function of the Collectors
class. This task uses the same simple class
Movie
for representing movie data as the previous task.
The class MovieAnalytics2
should have the following public features:
Constructor
MovieAnalytics2()
that initializes an empty movie database. E.g a simpleMovie
list is again fine.
Member function
void populateWithData(String fileName)
that reads movie data from the file spedicifed by the parameter.
The file is expected to contain lines of form “
title;releaseYear;duration;genre;score;director
”.
Member function
void printCountByDirector(int n)
that prints out then
directors that have directed the most movies, in descending order of the number of directed movies. Directors with equally many movies are ordered by their names (using the naturalString
order). Each director is output in the form “director: x movies
”, wherex
is the number of movies directed by that particular director.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 naturalString
order). Each genre is output in the form “genre: average
”, whereaverage
is the average duration of movies in that genre, using two decimals of precision.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 naturalString
order). Each genre is output in the form “genre: average
”, whereaverage
is the average rating score of movies in that genre, using two decimals of precision.
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. This restriction applies
also to reading the file contents, so implement also that part using a stream (ie. use the
BufferedReader
member function lines()
that creates 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 stupid 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 so that e.g. .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
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 e.g. as
javac *.java
, and run the tests as java MovieTest input1.txt 10
,
java MovieTest input2.txt 15
and java MovieTest 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.