Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For your next task, import the following pieces of code into your project folder: SetTest2.java (Links to an external site.) words.txt (Links to an external

For your next task, import the following pieces of code into your project folder:

  • SetTest2.java (Links to an external site.)
  • words.txt (Links to an external site.)
  • wizardOfOz.txt (Links to an external site.)
  • warAndPeace.txt (Links to an external site.)

Make sure that you have imported SetTest2 properly. SetTest2.java should be imported into your project's src folder, while the three text files should be imported into your main project folder. Look over the code in SetTest2 - trace through it and see if you can tell what it does. Run the code and provide the file "words.txt" to it and see what it does. Do the same for wizardOfOz.txt and warAndPeace.txt. This code stores the words from a file into a single List. Note the stub method getWordSet. Fill in the code for this method so that it collects the words in a Set instead of a List. Modify the main loop so that it runs twice - once to collect the words in a list and a second time to collect the words in a Set, and display the count of items in each. How are they different? Write your answer in the comments to this program. The stub in getWordSet uses a HashSet. Modify your main loop to iterate over the Set returned by getWordSet using an iterator and print the elements to the screen, then run your code and examine its behavior. Now modify getWordSet to use a TreeSet. How are the results from using a HashSet and TreeSet different? Write your answer in the comments at the top of the program. Now write a method named getWordCollection that uses the following method header:

public static void getWordCollection(String fname, Collection collection)

This method should fill the collection given to it with the Strings from the file named fname. The code for this method will be very similar to the code for the getWords method. Try passing this method different Collections that we have discussed so far (ArrayList, LinkedList, TreeSet, HashSet, Stack) - does it work for each of them? Which collections does it behave differently for? Which collections does it behave similarly for? Put your answers to these in the comments at the top of your code.

SetTest2.java

mport java.util.*; import java.io.*; public class SetTest2 { public static void main(String[] args) { // Get a file full of words from the user Scanner keyboard = new Scanner(System.in); System.out.print("Enter a filename: "); String fname = keyboard.nextLine(); // Assemble a list of all of the words in the file List myList = getWords(fname); // Print that list of words to the screen for (String s : myList) { System.out.println(s); } System.out.println("There are "+myList.size()+" words"); } /* * getWords * * Given a filename, reads all of the words out of that * file into a list, stripping punctuation. (Note that * this is not perfect - it will break the word * "teacher's" into "teacher" and "s", but it's a * simple tokenizer). * * @param fname - the name of the input file * @return a list of words from that file */ public static List getWords(String fname) { List tokenList = new ArrayList(); try { Scanner inFile = new Scanner(new File(fname)); // The useDelimiter method tells the Scanner what // to use as a breakpoint between words. Normally // it is whitespace. Here we set it to any character // that is NOT A-Z or a-z. The caret (^) indicates // "not". (This is known as a "regular expression", // and they show up quite a bit in Computer Science). inFile.useDelimiter("[^A-Za-z]"); while (inFile.hasNext()) { // NOTE: We're using "next" instead of "nextLine" // here. "next" reads until it reads a delimiter // character - in our case a non alphabetic // character. This will read in one word at a // time instead of one line at a time. String token = inFile.next(); // We only want to keep words that are not // empty strings. if (!token.equals("")) { tokenList.add(token); } } inFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tokenList; } /* * getWordSet * * Given a filename, reads all of the words out of that * file into a set, stripping punctuation. Note that * the word set will include only one element for each * word - no duplicate words - unlike the getWords method * above. * * @param fname - the name of the input file * @return a set of words from that file */ public static Set getWordSet(String fname) { Set tokenSet = new HashSet(); return tokenSet; } // public static void getWordCollection(String fname, Collection collect) { // // } }

Words.txt

the quick brown fox jumped over the lazy dog she sells sea shells by the sea shore over the river and through the woods, to grandmother's house we go.

WizardOfOz.txt

The Project Gutenberg EBook of The Wonderful Wizard of Oz, by L. Frank Baum

This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.net

Title: The Wonderful Wizard of Oz

Author: L. Frank Baum

Release Date: July 1, 2008 [EBook #55]

Language: English

*** START OF THIS PROJECT GUTENBERG EBOOK THE WONDERFUL WIZARD OF OZ ***

War&Peace.txt

The Project Gutenberg EBook of War and Peace, by Leo Tolstoy

This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.org

Title: War and Peace

Author: Leo Tolstoy

Posting Date: January 10, 2009 [EBook #2600] Release Date: April, 2001 [Last updated: August 22, 2012]

Language: English

*** START OF THIS PROJECT GUTENBERG EBOOK WAR AND PEACE ***

An Anonymous Volunteer

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

Students also viewed these Databases questions

Question

=+Does it showcase the firm's benefits?

Answered: 1 week ago