Question: Start code: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Collections; /* Starter code for PS8. */ public

 Start code: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.List; import

Start code:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.util.List;

import java.util.ArrayList;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import java.util.Collections;

/* Starter code for PS8.

*/

public class RankMovies

{

public static void main(String[] args)

{

File file = new File("../resource/asnlib/publicdata/ratings-utf8");

ArrayList rl = new ArrayList();

try

{

Scanner scanner = new Scanner(file);

while (scanner.hasNextLine())

{

/* Read in lines, using fact that the

* title starts in column 32, and the

* other information is separated by

* 1 or more blanks.

*/

String line = scanner.nextLine();

String pre = line.substring(0, 31);

String post = line.substring(32);

String[] tkns = pre.split("\\s+");

MovieRating nr = new MovieRating(tkns[2], tkns[3], post);

rl.add(nr);

}

scanner.close();

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

int minVotes = 1;

int numRecords = 1;

Scanner input = new Scanner(System.in);

while (true)

{

System.out.println();

System.out.println("Enter minimum vote threshold and number of records:");

minVotes = input.nextInt();

numRecords = input.nextInt();

if (minVotes * numRecords == 0)

{

break;

}

long startTime = System.currentTimeMillis();

/* Fill in code to determine the top numRecords movies that have at least

* minVotes votes. For each record mr, in decreaseing order of average rating,

* execute a System.out.println(mr).

* Do not sort the movie list!

*/

System.out.println();;

long readTime = System.currentTimeMillis();

System.out.println("Time: "+(System.currentTimeMillis()-startTime)+" ms");

}

}

}

------------------------------------------------

import java.util.LinkedList;

import java.util.Scanner;

public class MovieRating implements Comparable

{

private int votes;

private float rating;

private String title;

MovieRating(String v, String r, String t)

{

votes = Integer.parseInt(v);

rating = Float.parseFloat(r);

title = t;

}

public int getVotes()

{

return votes;

}

public float getRating()

{

return rating;

}

public String getTitle()

{

return title;

}

public String toString()

{

String s = Integer.toString(votes)+" "+

Float.toString(rating) + " " + title;

return s;

}

public int compareTo(MovieRating m)

{

if(this.rating - m.rating > 0.0)

{

return 1;

}

else if(this.rating - m.rating

{

return -1;

}

else

{

return 0;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!