(This is a Java program not a c++ program) Use the ArrayList, and TreeSet to sol
ID: 3626278 • Letter: #
Question
(This is a Java program not a c++ program)Use the ArrayList, and TreeSet to solve a specific problem
create a class named ListTreeSet that performs the following operations on the contents of a text file containing the address given by President Abraham Lincoln at Gettysburg, Pennsylvania 19 November 1863:
1. Tokenize the address into individual words and allow display to the screen (use a delimiter when reading the file).
2. Allow reporting of the total number of words in the address.
3. Sort the words of the address while removing duplicates words then display to the screen.
4. Report the number of duplicate words found in the original address.
5. Search the original address for the word “brave” (without quotes) and report its word position within the address.
6. Display the entire address token-by-token, reversed.
All of these operations must be exercised by the “user” class main method. The user class should perform the displaying of data provided by your ListTreeSet class. Your ListTreeSet class should NOT display anything.
Explanation / Answer
ListTreeSet.java
import java.io.*;
import java.util.*;
public class ListTreeSet {
private TreeSet<String> wordSet;
private ArrayList<String> wordList;
public ListTreeSet(String filename) {
wordSet = new TreeSet<String>();
wordList = new ArrayList<String>();
Scanner sc;
try {
sc = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
System.out.println("File not found: "+filename);
return;
}
while (sc.hasNext()) {
String nextWord = sc.next();
wordList.add(nextWord);
// changing to lower case ensures that capitalized words
// will be considered the same as their lower-case versions
// for the purpose of removing duplicates.
// If this isn't what you want then remove this line.
nextWord = nextWord.toLowerCase();
wordSet.add(nextWord);
}
}
public int wordCount() {
return wordList.size();
}
public List<String> getWordList() {
return new ArrayList<String>(wordList);
}
public TreeSet<String> getWordSet() {
return new TreeSet<String>(wordSet);
}
// return the number of words that have duplicates earlier in the address
// e.g. "X X X" is considered to have two duplicates (the first X is not counted as a duplicate)
// I wasn't sure if this is what was required, the question was unclear.
public int duplicateWordCount() {
return wordList.size() - wordSet.size();
}
public int indexOf(String word) {
return wordList.indexOf(word);
}
public List<String> reversedList() {
List<String> backwards = new ArrayList<String>(wordList);
Collections.reverse(backwards);
return backwards;
}
}
--------------------------------------
GettysburgInfo.java
public class GettysburgInfo {
public static void main(String[] args) {
String filename = "gettysburg.txt";
ListTreeSet lts = new ListTreeSet(filename);
System.out.println("Word tokens in the Gettysburg Address:");
for (String word : lts.getWordList()) System.out.print(word+" ");
System.out.println(); System.out.println();
System.out.println("Number of words: "+lts.wordCount());
System.out.println();
System.out.println("Word tokens sorted and with duplicates removed: ");
for (String word : lts.getWordSet()) System.out.print(word+" ");
System.out.println();
System.out.println("Number of duplicate words:"+lts.duplicateWordCount());
System.out.println();
System.out.println("Position of the word 'brave':"+lts.indexOf("brave"));
System.out.println();
System.out.println("Gettysburg address, reversed: ");
for (String word : lts.reversedList()) System.out.print(word+" ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.