Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE PROVIDE CODE THAT WORKS, EVERYTHING NEEDED TO COMPLETE IS HERE!!!! All the input files needed and the output required is at https://venus.cs.qc.edu/~mfried/cs313/hw/hw2.html, it will

PLEASE PROVIDE CODE THAT WORKS, EVERYTHING NEEDED TO COMPLETE IS HERE!!!!

All the input files needed and the output required is at https://venus.cs.qc.edu/~mfried/cs313/hw/hw2.html, it will say website unprotected just press advanced and proceed to website it is safe. Need this asap!!!!

Three of the methods in HW2.java are incomplete. Write the code so it works correctly. You should submit one file, HW2.java. Do not change the method headers. Each method should print the output to the console. They should not return anything. The purpose of this assignment is to learn how to use the JCF classes (HashSet, HashMap, TreeSet, TreeMap). You do not need to implement these classes. public static List getList(String filename) This method has been done for you. It takes a filename as a parameter and returns a List of movies. Each file contains one movie per line. 3_lists.txt has duplicates because it contains 3 lists (the other files do not have duplicates). public static void intersection(List list1, List list2) Print all movies that occur in both lists. The order is not important. The expected runtime must be O(n) where n is the total number of movies in the lists. Do not sort the movies because it is too slow. Use a HashSet to store one of the lists, so you can search the HashSet efficiently. You may assume that a string can be inserted into a hash table in expected O(1) time. public static void frequent(List list, int k) Print all movies in the list that occur at least k times. The order is not important. Print the movie followed by the number of occurrences in parentheses, as in the sample output. The expected runtime must be O(n) where n is the total number of movies in the list. Do not sort the movies. Use a HashMap to count occurrences (the key is the movie, the value is the number of occurrences). For each movie, check if it's a key in the map. If it is, update its value. Otherwise add a new entry to the map. Then iterate through the map and print the frequent movies. See UsingSetsMaps for an example of how use a for-each loop with a map. public static void groupByNumChars(List list) Print all movies in the list, grouped by number of characters. All movies with the same number of characters should be printed on the same line, and movies with fewer characters should be printed first. The runtime must be O(nlogn) where n is the number of movies in the list. There is more than one way to solve this problem, but one way is to use a map. The key is the number of characters, and the value is a list of movies with that number of characters. You can declare it like this: Map> map = new ... // Choose the appropriate map class For each movie, you can use the String class's length function to get the number of characters. If the map doesn't already have an entry for this length, create a new entry (use the map's put method). If the map already has an entry for this length, first call the map's get method (which returns a reference to a list), then add the movie to the list. Do not use a single String to store more than one movie, because it will slow down the runtime (but a StringBuilder is ok).

____________________________________________________________________________________________________________________

//HW2.java import java.io.*; import java.util.*; public class HW2 { // Prints all movies that occur in both lists. public static void intersection(List list1, List list2) { // Fill in. } // Prints all movies in the list that occur at least k times // (print the movie followed by the number of occurrences in parentheses). public static void frequent(List list, int k) { // Fill in. } // Prints all movies in the list, grouped by number of characters. // All movies with the same number of characters are printed on the same line. // Movies with fewer characters are listed first. public static void groupByNumChars(List list) { // Fill in. } // Returns a List of all movies in the specified file (assume there is one movie per line). public static List getList(String filename) { List list = new ArrayList<>(); try (Scanner in = new Scanner(new FileReader(filename))) { while (in.hasNextLine()) { String line = in.nextLine(); list.add(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } return list; } public static void main(String[] args) { List list1 = getList("imdb.txt"); List list2 = getList("sight_and_sound.txt"); List list3 = getList("3_lists.txt"); System.out.println("***intersection***"); intersection(list1, list2); System.out.println("***frequent***"); frequent(list3, 3); System.out.println("***groupByNumChars***"); groupByNumChars(list3); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions