In: Computer Science
Purpose
Time Estimate
4-5 hours to complete.
Background
You have been given the following:
Code
Input files
Sample output
The sample output is produced for the provided main function; mainly, the following cases:
// intersection System.out.println(setAndMaps.intersection(list1, list2)); // frequent System.out.println(setAndMaps.frequent(list3, 3)); // groupByNumChars System.out.println(setAndMaps.groupByNumChars(list2));
Problem Statement
Three of the methods in setAndMaps.java are incomplete. Write the code so it works correctly. You should only code in one file, setAndMaps.java.
Do not change the method signatures. Each method should return the output as a String.
Code Structure
public static List getList(String filename){};
public static String intersection(List list1, List list2){};
public static String frequent(List list, int k){};
public static String groupByNumChars(List list){};
Advice
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.
Do not use a single String to store more than one movie, because it will slow down the runtime
Requirements
The program takes in text files that are then converted to lists. Your solutions will operate on the parameterized lists.
The code must properly complete the setAndMaps.java class and work with the provided Main.java class.
All functions in the setAndMaps.java class MUST be implemented.
All code you write should exist solely in the specified methods and additional helper functions. NO CODE SHOULD BE ADDED TO THE MAIN FUNCTION
Base your code on the given template. Any code not adhering to the given template may work, but may not pass all tests upon submission.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Main {
// Returns a List of all movies in the specified file
(assume there is one movie per line).
public static ArrayList getList(String filename)
{
ArrayList 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) {
ArrayList list1 =
getList("imdb.txt");
ArrayList list2 =
getList("sight_and_sound.txt");
ArrayList list3 =
getList("3_lists.txt");
//Sort lists
Collections.sort(list1);
Collections.sort(list2);
Collections.sort(list3);
System.out.println("***\nintersection\n***");
System.out.println(setAndMaps.intersection(list1, list2));
System.out.println("***\nfrequent\n***");
System.out.println(setAndMaps.frequent(list3, 3));
System.out.println("***\ngroupByNumChars\n***");
System.out.println(setAndMaps.groupByNumChars(list2));
}
}
import java.util.*;
BELOW IS WHERE WE DO THE CODE
public class setAndMaps {
// Prints all movies that occur in both lists.
public static String intersection(List<String> list1,
List<String> list2) {
//Add code below
return "";
}
// 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 String frequent(List<String> list, int k)
{
//Add code below
return "";
}
// 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 String groupByNumChars(List<String> list)
{
//Add code below
return "";
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Main {
//Array list of movies
public static ArrayList<String>
getList(String filename) {
ArrayList<String>
arrayList = new ArrayList<>();
try (Scanner scanner =
new Scanner(new FileReader(filename))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
arrayList.add(line);
}
} catch
(FileNotFoundException exception) {
exception.printStackTrace();
}
return arrayList;
}
//main method
public static void main(String[] args) {
ArrayList<String>
list1 =
getList("/Users/swapnil/IdeaProjects/MovieList/src/imdb.txt");
ArrayList<String>
list2 =
getList("/Users/swapnil/IdeaProjects/MovieList/src/sight_and_sound.txt");
ArrayList<String>
list3 =
getList("/Users/swapnil/IdeaProjects/MovieList/src/3_lists.txt");
//Sort lists
Collections.sort(list1);
Collections.sort(list2);
Collections.sort(list3);
System.out.println("My test" + SetAndMaps.groupByNumChars(list3));
System.out.println("***\nintersection\n***");
System.out.println(SetAndMaps.intersection(list1, list2));
System.out.println("***\nfrequent\n***");
System.out.println(SetAndMaps.frequent(list3, 3));
System.out.println("***\ngroupByNumChars\n***");
System.out.println(SetAndMaps.groupByNumChars(list2));
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class SetAndMaps {
// Prints all movies present in the both the
files/ lists
public static String
intersection(List<String> list1, List<String> list2)
{
Set<String>
movieList1 = new HashSet<>(list1);
Set<String>
movieList2 = new HashSet<>(list2);
Set<String>
intersection = new HashSet<>();
if(movieList1.size()
< movieList2.size() ){
intersection = movieList2;
intersection.retainAll(movieList1);
}else{
intersection = movieList1;
intersection.retainAll(movieList2);
}
ArrayList<String> matchingVals = new
ArrayList<>();
for (String element :
intersection) {
matchingVals.add(element);
}
Collections.sort(matchingVals); //nlogn
String result =
"";
for (String element :
matchingVals) {
result = result + element;
result = result + "\n";
}
return result;
}
// method to prints all movies that occur
frequent
public static String frequent(List<String>
list, int k) {
Map<String,
Integer> movieTMap = new TreeMap<String, Integer>();
for (String element :
list) {
Integer num = movieTMap.get(element);
if (num == null) movieTMap.put(element, 1);
else movieTMap.put(element, num + 1);
}
String result =
"";
for
(Map.Entry<String, Integer> entry : movieTMap.entrySet())
{
if (entry.getValue() >= k) {
result = result + entry.getKey() + "(" + entry.getValue() +
")";
result = result + "\n";
}
}
return result;
}
//method to print the movies grouped by
number of characters.
public static String
groupByNumChars(List<String> list) {
Map<Integer,
List<String>> movieMap = new TreeMap<Integer,
List<String>>();
for (String element :
list) {
int count = 0;
for (int i = 0; i < element.length(); i++) {
count++;
}
List<String> movieNames = movieMap.get(count);
if (movieNames == null) {
movieNames = new ArrayList<String>();
movieNames.add(element);
movieMap.put(count, movieNames);
}
else {
if (!movieNames.contains(element)) movieNames.add(element);
}
}
String result =
"";
for
(Map.Entry<Integer, List<String>> entry :
movieMap.entrySet()) {
ArrayList<String> temp = (ArrayList<String>)
entry.getValue();
Collections.sort(temp);
result = result + temp + "\n";
}
return result;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Program Output :