Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Ex-Collections and Iterators DictionaryTest.java Collection Interface import java.awt.HeadlessException; import java.io.FileNotFoundException; You will run this program four times, performing experiments on four classes that implement the Collection interface. import javax.swing.JOptionPane; public class DictionaryTest { public static void main(String[] args) { try { String fileName = JOptionPane.showInputDialog("Dictionary: "); Dictionary dictTest = new Dictionary(fileName); // The following code tests the behaviour of the Dictionary object dictTest.loadDictionary(); JOptionPane.showMessageDialog(null, "Loading finished. Click OK to display first 100 words . . ."); dictTest.display(100); JOptionPane.showMessageDialog(null, "Display finished. Click OK to begin searching process . . ."); dictTest.searchAllWords(); On each run, uncomment one of the four classes. Gather timing and size information for each class and fill in the blanks on this page. Use Main.txt for your timing. It’s a text file that contains nearly 80,000 Englishlanguage words. } catch (HeadlessException e) { // can be generated by JOptionPane.showInputDialog() e.printStackTrace(); } catch (FileNotFoundException e) { System.out.println("File not found."); } } } Dictionary.java import java.io.File; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Scanner; import javax.swing.JOptionPane; ArrayList Load Time: ____________ Search Time:____________ Number of Words Loaded: ______________________ public class Dictionary { (You’ll need to uncomment and add the import.) private String fileName; private Collection<String> collectionWords; // The collection type is not defined here; it will be created elsewhere. LinkedList Load Time: ____________ Search Time:____________ /** Constructor establishes the association with a disk-based file, so that all tests are performed on the same set of data. */ public Dictionary(String fileName) { this.fileName = fileName; } Number of Words Loaded: ______________________ /** * Uses the predefined filename to open a file and populate the collection sent by reference to this method. * @throws FileNotFoundException (You’ll need to uncomment and add the import.) */ public void loadDictionary() throws FileNotFoundException { Load Time: ____________ Search Time:____________ // TODO Run the program four times. Each time, capture a different Collection class in "collectionWords" collectionWords = new ArrayList<String>(); // collectionWords = new LinkedList<String>(); Number of Words Loaded: ______________________ // collectionWords = new TreeSet<String>(); // collectionWords = new HashSet<String>(); Scanner scanInput = new Scanner(new File(fileName)); long startTime = System.currentTimeMillis(); (You’ll need to uncomment and add the import.) while (scanInput.hasNext()) { String sWord = scanInput.nextLine(); Load Time: ____________ Search Time:____________ collectionWords.add(sWord); // virtual method to add another word to the collection } Number of Words Loaded: ______________________ scanInput.close(); double elapseTime = (double) (System.currentTimeMillis() - startTime)/1000.0; System.out.printf("Collection Type:%s loadDictionary() Word Count:%d Elapse Time:%.3f seconds\n", collectionWords.getClass().getName(), collectionWords.size(), elapseTime); } TreeSet HashSet /** * Iterates through every word in the collection, looking up each in the entire collection. * @return time (in seconds) to complete the end-to-end search process. */ public void searchAllWords() { System.out.print("\n\nDisplaying one dot per thousand matches"); long startTime = System.currentTimeMillis(); int numMatches = 0; for (String s : collectionWords) { if (collectionWords.contains(s)) ++numMatches; Note that an enhanced for loop has only 2 parts (unlike a standard for if (numMatches%1000 == 0) loop that has 3). The second part must be a reference to some kind of System.out.print("."); } collection. The first component will be a reference-to successive objects double elapseTime = (double) (System.currentTimeMillis() - startTime)/1000.0; found in theNumber collection. The enhanced for loop employs a hidden iterator. System.out.printf("\nCollection Type:%s searchAllWords() Elapse Time:%.3f of Matches:%d\n\n", collectionWords.getClass().getName(), elapseTime, numMatches); } Enhanced for Loop Employing Implicit Iterator /** * Iterates through the first limitOfDisplay words and displays on the console. * @param limitOfDisplay */ public void display(int limitOfDisplay) { System.out.printf("\nFirst %d words in %s, organized using %s: \n", limitOfDisplay, fileName, collectionWords.getClass().getName()); System.out.printf("Word list generated using an enhanced for-loop (implicit iterator): \n"); int wordCount = 0; // iterate through the collection using an enhanced for loop (with an implicit iterator) for (String s : collectionWords) { System.out.print(s); System.out.print(" "); if (++wordCount % 20 == 0) // newline every 20 words Another use of an enhanced for loop. System.out.println(); if (wordCount >= limitOfDisplay) break; } Enhanced for Loop Employing Implicit Iterator // iterate through the collection using a while loop (with an explicit iterator) System.out.printf("\n\nSame word list generated using a while-loop (explicit iterator): \n"); wordCount = 0; Iterator<String> iter = collectionWords.iterator(); while (iter.hasNext()) { String s = iter.next(); System.out.print(s); System.out.print(" "); This is an ordinary while loop. An Iterator object is explicitly retrieved from if (++wordCount % 20 == 0) // newline every 20 words the Collection (one of ArrayList, LinkedList, TreeSet or HashSet). The System.out.println(); Iterator object knows if there are more items in the Collection and how to if (wordCount >= limitOfDisplay) break; move from one to the next. } Ordinary while Loop with Explicit Iterator } } Reference-to-Object Layout Exercise Marking Guide: Full Marks: 2 Choose one of the following Collection classes: TreeSet, LinkedList and HashSet. Sketch a memory map showing the organization of memory after the 15 words are loaded from Small.txt. ① Reasonable attempt, but some issues. Note: Your two memory map choices must be something other than an ArrayList. Name: ___________________ ② Essentially correct, and shown by end of lab period. ③ (Bonus) Essentially correct, and shown before (or at) the start of the lab period.