Question: LongestFinder.java package edu.csc220.recursion; import java.util.ArrayList; public class LongestFinder { /** Returns the longest String in the list words. If there is a tie, any of
LongestFinder.java
package edu.csc220.recursion; import java.util.ArrayList; public class LongestFinder { /** Returns the longest String in the list words. If there is a tie, any of the Strings can be returned. */ public String findLongest(ArrayList words) { // TODO: Implement this. String longest = ""; for (int i = 0; i < words.size(); i++) if (words.get(i).length() > longest.length()) longest = words.get(i); return longest; } public static void main(String[] args) { LongestFinder longestFinder = new LongestFinder(); ArrayList words = new ArrayList<>(); words.add("apple"); words.add("banana"); words.add("cat"); words.add("dog"); words.add("elephant"); words.add("fish"); words.add("gin"); // Expected to print out "Longest: elephant". System.out.println("Longest: " + longestFinder.findLongest(words)); } } WordSplitChecker.java
package edu.csc220.recursion; import java.io.*; import java.util.*; public class WordSplitChecker { /** * Returns true if there is at least one way to split input into a sequence of valid English words (words that * appear in the allWords set), and false otherwise. * * For this assignment, you can define your own private helper method, but you cannot change this method's signature * (i.e. the name, the parameters, or the fact that it returns a boolean). */ public boolean hasWordSplit(String input, TreeSet allWords) { // TODO: Implement this. return false; } public static void main(String[] args) { TreeSet allWords = readWords(); WordSplitChecker wordSplitChecker = new WordSplitChecker(); // All of the following should print out true. System.out.println("goat: " + wordSplitChecker.hasWordSplit("goat", allWords)); System.out.println("car: " + wordSplitChecker.hasWordSplit("goat", allWords)); System.out.println("isawabus: " + wordSplitChecker.hasWordSplit("isawabus", allWords)); // All of the following should print out false. System.out.println("xvix: " + wordSplitChecker.hasWordSplit("xvix", allWords)); System.out.println("m: " + wordSplitChecker.hasWordSplit("m", allWords)); } // Reads the "words.txt" file and returns the words in a TreeSet. This is completely implemented for you! private static TreeSet readWords() { TreeSet allWords = new TreeSet<>(); try { BufferedReader bufferedReader = new BufferedReader(new FileReader("words.txt")); String line; while ((line = bufferedReader.readLine()) != null) { if (line.trim().isEmpty()) { continue; } allWords.add(line.toLowerCase()); } bufferedReader.close(); } catch (IOException exception) { throw new RuntimeException(exception); } return allWords; } }
WordSplitPrinter.java
package edu.csc220.recursion; import java.io.*; import java.util.*; public class WordSplitPrinter { /** * Finds and prints out every possible way to split the input String into individual, valid English words (including * if input is itself a single valid word). You must call printWordSplit below to actually print out the results. */ public void findWordSplits(String input, TreeSet allWords) { // TODO: Implement this. } /** Prints out a word split, i.e. one particular arrangement of words. This is implemented for you! */ private void printWordSplit(ArrayList words) { if (words.isEmpty()) { System.out.println("(empty word list)"); } else { System.out.print(words.get(0)); for (int i = 1; i < words.size(); i++) { System.out.print(" " + words.get(i)); } System.out.println(); } } public static void main(String[] args) { TreeSet dictionary = readWords(); WordSplitPrinter wordSplitPrinter = new WordSplitPrinter(); // Expected to print out: // i saw a bus // i saw ab us // is aw a bus // is aw ab us wordSplitPrinter.findWordSplits("isawabus", dictionary); } // Reads the "words.txt" file and returns the words in a TreeSet. This is completely implemented for you! private static TreeSet readWords() { TreeSet allWords = new TreeSet<>(); try { BufferedReader bufferedReader = new BufferedReader(new FileReader("words.txt")); String line; while ((line = bufferedReader.readLine()) != null) { if (line.trim().isEmpty()) { continue; } allWords.add(line.toLowerCase()); } bufferedReader.close(); } catch (IOException exception) { throw new RuntimeException(exception); } return allWords; } }
Fill in TODOs. Thank you!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
