Question

9. A concordance is an alphabetical word list for a passage of text. Each word in the concordance is mapped to an integer indicating the frequency of the words occurrence. The constructor of Concordance has one String parameter that identifies the text file to be read. An incompleteConcordance class is below. public class Concordance public Concordance (String nameOfFileH concord new TreeMap<String, Integer 0 createConcordance (nameOfFile); //Constructor //nameOfFile the text file being read public void createConcordance (string filename)...) //Create a TreeMap of words mapped to their Integer frequencies public void printConcordance (... //Prints the alphabetized list of words paired with their frequencies private Map<String, Integer> concord; The method createConcordance will fill concord, a TreeMap of words mapped to their Integer frequencies. Complete the method createConcordance below. You may assume that all words in the passage are made up of lowercase letters. a. public void createConcordance (String filename) while (inFile.hasNext 0M//while more words to read String word- //read word from file // More code goes here
media%2F6e6%2F6e60f5c1-19ce-4e44-a36e-e7
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE TO COPY:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Map;

import java.util.Scanner;

import java.util.Set;

import java.util.TreeMap;

public class Concordance {

public Concordance(String nameOfFile) throws FileNotFoundException

{

concord=new TreeMap<String, Integer>();

createConcordance(nameOfFile);

}

public void createConcordance(String fileName) throws FileNotFoundException

{

File file=new File(fileName);

Scanner obj=new Scanner(file);

while(obj.hasNext())

{

String word=obj.next();

if(!concord.containsKey(word))

concord.put(word,1);

else {

int count=concord.get(word);

count++;

concord.put(word, count);

}

}

}

public void printConcordance()

{

Set<String> set=concord.keySet();

for(String word:set)

{

System.out.println(word+" occurs "+concord.get(word)+" time(s)");

}

}

private Map<String, Integer> concord;

public static void main(String[] args) throws FileNotFoundException {

Concordance concordance=new Concordance("words.txt");

concordance.printConcordance();

}

}

PROGRAM SCREENSHOTS:

INPUT FILE:

OUTPUT:

In case of any doubt, please let me know in the comments section. I'll get it resolved :) :)

Add a comment
Know the answer?
Add Answer to:
9. A concordance is an alphabetical word list for a passage of text. Each word in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...

    Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. With your code, you’ll read from the text file and capture the data into a data structure. Using a data structure, write the code to count the appearance of each unique word in the lyrics. Print out a word frequency list. Example of the word frequency list: 100: frog 94: dog 43: cog 20: bog Advice: You can...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Using the diagram below and the starter code, write Document Processor that will read a file...

    Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...

  • Lab Description Sort all words by comparing the length of each word. The word with the...

    Lab Description Sort all words by comparing the length of each word. The word with the smallest length would come first. If you have more than one word with the same length, that group would be sorted alphabetically Input: The data file contains a list of words. The first line in the data file is an integer that represents the number of data sets to follow Output: Output the complete list of words in order by length. Sample Data 10...

  • out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs...

    out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs to be handled and is specifically the part I am having trouble with https://www.dropbox.com/s/ume3slnphyhi6wt/out1.txt?dl=0 - link for the out1.txt file for testing yourself Please include comments as I am in this for learning and would really appreciate the help! Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT