Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Laramie Park District has files of participants in its summer and winter program

ID: 665990 • Letter: L

Question

Laramie Park District has files of participants in its summer and winter programs this year.Each file is in participant ID number order and contains additional fields for first name, last name, age and class taken (e.g. Beginning Swimming).

A. Design the logic for a program that merges the files for summer and winter programs to create a list of the first and last names of all participants for the year.

B. Modify the program so that if a participant has more than one record, you output the participant's name only once.

C. Modify the program so that if a participant has more than one record, you output the name only once, but you also output the count of the total number of classes the participant has taken.

Explanation / Answer

package summerwinter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;

public class MergerFiles {

   public static void main(String[] args) {
       String sourceFile1Path = "/home/programcreek/Desktop/s1/summer.txt";
       String sourceFile2Path = "/home/programcreek/Desktop/s2/winter.txt";
       String mergedFilePath = "/home/programcreek/Desktop/m/result.txt";
      
      
       Map<String, Integer> wordMap = getWordCount("C:/MyTestFile.txt");
        List<Entry<String, Integer>> list = sortByValue(wordMap);
        for(Map.Entry<String, Integer> entry:list){
            System.out.println(entry.getKey()+" ==== "+entry.getValue());
        }
   }
   public void getMergedFile(String sourceFile1Path,String sourceFile2Path,String mergedFilePath)
   {
      
       File[] files = new File[2];
       files[0] = new File(sourceFile1Path);
       files[1] = new File(sourceFile2Path);

       File mergedFile = new File(mergedFilePath);

       mergeFiles(files, mergedFile);
   }

   public static void mergeFiles(File[] files, File mergedFile) {

       FileWriter fstream = null;
       BufferedWriter out = null;
       try {
           fstream = new FileWriter(mergedFile, true);
           out = new BufferedWriter(fstream);
       } catch (IOException e1) {
           e1.printStackTrace();
       }

       for (File f : files) {
           System.out.println("merging: " + f.getName());
           FileInputStream fis;
           try {
               fis = new FileInputStream(f);
               BufferedReader in = new BufferedReader(new InputStreamReader(fis));

               String aLine;
               while ((aLine = in.readLine()) != null) {
                   out.write(aLine);
                   out.newLine();
               }

               in.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

       try {
           out.close();
       } catch (IOException e) {
           e.printStackTrace();
       }

   }
  
   public static List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){
       
            Set<Entry<String, Integer>> set = wordMap.entrySet();
            List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
            Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
            {
                public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
                {
                    return (o2.getValue()).compareTo( o1.getValue() );
                }
            } );
            return list;
        }
   public static Map<String, Integer> getWordCount(String fileName){
      
            FileInputStream fis = null;
            DataInputStream dis = null;
            BufferedReader br = null;
            Map<String, Integer> wordMap = new HashMap<String, Integer>();
            try {
                fis = new FileInputStream(fileName);
                dis = new DataInputStream(fis);
                br = new BufferedReader(new InputStreamReader(dis));
                String line = null;
                while((line = br.readLine()) != null){
                    StringTokenizer st = new StringTokenizer(line, " ");
                    while(st.hasMoreTokens()){
                        String tmp = st.nextToken().toLowerCase();
                        if(wordMap.containsKey(tmp)){
                            wordMap.put(tmp, wordMap.get(tmp)+1);
                        } else {
                            wordMap.put(tmp, 1);
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                try{if(br != null) br.close();}catch(Exception ex){}
            }
            return wordMap;
        }
  
}