Question
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
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
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");
}
}
}
Extra Code:
import java.util.ArrayList;
import java.util.Arrays;
/** Max-heap implementation */
public class MaxHeap
private ArrayList
private int n; // Number of things in heap
/** Constructor for initially empty heap */
public MaxHeap() {
Heap = new ArrayList
n = 0;
}
public MaxHeap(E[] h) {
Heap = new ArrayList
n = h.length;
buildheap();
}
/** @return Current size of the heap */
public int heapsize() {
return n;
}
public boolean isEmpty() {
return n == 0;
}
/** @return True if pos a leaf position, false otherwise */
public boolean isLeaf(int pos) {
return (pos >= n / 2) && (pos
}
/** @return Position for left child of pos */
public int leftchild(int pos) {
assert 2 * pos + 1
return 2 * pos + 1;
}
/** @return Position for right child of pos */
public int rightchild(int pos) {
assert 2 * pos + 2
return 2 * pos + 2;
}
/** @return Position for parent */
public int parent(int pos) {
assert pos > 0 : "Position has no parent";
return (pos - 1) / 2;
}
/** Insert val into heap */
public void insert(E val) {
Heap.add(val); // Start at end of heap
int curr = n;
n++;
// Now sift up until curr parents key > currs key
while ((curr != 0)
&& (Heap.get(curr).compareTo(Heap.get(parent(curr))) > 0)) {
swap(curr, parent(curr));
curr = parent(curr);
}
}
/** Heapify contents of Heap */
public void buildheap() {
for (int i = n / 2 - 1; i >= 0; i--)
siftdown(i);
}
/** Put element in its correct place */
private void siftdown(int pos) {
assert (pos >= 0) && (pos
while (!isLeaf(pos)) {
int j = leftchild(pos);
if ((j
j++; // j is now index of child with greater value
if (Heap.get(pos).compareTo(Heap.get(j)) >= 0)
return;
swap(pos, j);
pos = j; // Move down
}
}
/** Remove and return maximum value */
public E removemax() {
assert n > 0 : "Removing from empty heap";
swap(0, --n); // Swap maximum with last value
if (n != 0) // Not on last element
siftdown(0); // Put new heap root val in correct place
return Heap.get(n);
}
private void swap(int i, int j) {
E temp = Heap.get(j);
Heap.set(j, Heap.get(i));
Heap.set(i, temp);
}
}
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started