It Ruined Any Hope of France Ever Again Being Either Internally United or Externally Respected

Lab nine: Sets in the Java Collection Framework For this week'due south lab,...

Lab 9: Sets in the Coffee Collection Framework

For this week's lab, you lot volition apply ii of the classes in the Java Collection Framework:HashSet andTreeSet. Y'all will use these classes to implement a spell checker.

Set Methods

this lab, you will need to use some of the methods that are defined in the Prepare interface. Recall that if set is a Gear up, then the following methods are defined:

  • set.size() -- Returns the number of items in the set up.
  • set.add together(particular) -- Adds the detail to the set, if it is non already there.
  • fix.contains(item) -- Check whether the prepare contains the item.
  • fix.isEmpty() -- Check whether the set is empty.

You will also demand to be able to traverse a set, using either an iterator or a for-each loop.

Reading a Dictionary

The file words.txt (in the code directory) contains a list of English words, with i word on each line. You lot volition look up words in this list to cheque whether they are correctly spelled. To make the list easy to use, yous tin shop the words in a gear up. Since there is no need to have the words stored in society, yous can use aHashSet for maximum efficiency.

Use a Scanner to read the file. You tin create scanner,filein, for reading from a file with a argument such every bit:

                     filein = new Scanner

(new File("/classes/s09/cs225/words.txt"));

and that a file can exist processed, token by token, in a loop such as:

while (filein.hasNext()) {

     String tk = filein.next();

     process(tk); // do something with the token

}

(For the wordlist file, a token is simply a word.)

Start your main programme by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this program,convert all words to lower case before putting them in the ready. To make sure that you've read all the words, check the size of the set. (Information technology should be 72875.) You could also use the contains method to cheque for the presence of some common discussion in the set.

Checking the Words in a File

One time you lot accept the list of words in a set, it'south like shooting fish in a barrel to read the words from a file and check whether each discussion is in the prepare. Start by letting the user select a file. You can either permit the user blazon the name of the file or y'all can utilize the following method:

        /**

         * Lets the user select an input file using a standard file

         * selection dialog box. If the user cancels the dialog

         * without selecting a file, the return value is null.

         */

        static File getInputFileNameFromUser() {

           JFileChooser fileDialog = new JFileChooser();

           fileDialog.setDialogTitle("Select File for Input");

           int pick = fileDialog.showOpenDialog(cypher);

           if (selection != JFileChooser.APPROVE_OPTION)

              return null;

           else

              render fileDialog.getSelectedFile();

        }

Utilise a Scanner to read the words from the selected file. In social club to skip over any not-letter characters in the file, y'all can use the post-obit command just after creating the scanner (where in is the variable name for the scanner):

in.useDelimiter("[^a-zA-Z]+");

(In this statement, "[^a-zA-Z]+" is a regular expression that matches any sequence of one or more non-letter characters. This essentially makes the scanner treat any not-letter of the alphabet the style it would ordinarily treat a space.)

You can then become through the file, read each word (converting it to lower case) and check whether the set contains the word. At this point, but impress out whatever word that you find that is non in the dictionary.

Providing a List of Possible Right Spellings

A spell checker shouldn't just tell you what words are misspelled -- it should also requite you lot a list of possible correct spellings for that word. Write method

static TreeSet corrections(String badWord, HashSet dictionary)

that creates and returns aTreeSet<String> containing variations onbadWord that are contained in the dictionary. In your main program, when yous find a discussion that is not in the prepare of legal words, pass that word to this method (along with the ready). Take the return value and output whatever words that it contains; these are the suggested correct spellings of the misspelled give-and-take. Here, for example, is part of the output from a sample program when it was run with the HTML source of this page as input:

html: (no suggestions)

cpsc: (no suggestions)

hashset: hash set

treeset: tree set

cvs: cs, vs

isempty: is empty

href: ref

txt: tat, tet, text, tit, tot, tut

filein: file in

pre: are, ere, ire, ore, pare, pee, per, pie, poe, pore, prep, pres,

      prey, pro, pry, pure, pyre, re

hasnext: has next

wordlist: word list

getinputfilenamefromuser: (no suggestions)

jfilechooser: (no suggestions)

filedialog: file dialog

setdialogtitle: (no suggestions)

int: pismire, dint, hint, in, ina, inc, ind, ink, inn, ins, inti, into,

     information technology, lint, mint, nit, pint, tint

Note that the program was written and so that information technology will not output the same misspelled word more than than once. (This is done by keeping a fix of misspelled words that have been output.) If thecorrections() method returns an empty set, the plan outputs the bulletin "(no suggestions)". Since the corrections are stored in a tree fix, they are automatically printed out in alphabetical order with no repeats.

The possible corrections that the program considers are every bit follows:

• Delete any one of the letters from the misspelled word.

• Change any letter in the misspelled discussion to any other letter.

• Insert any letter of the alphabet at whatever point in the misspelled word.

• Swap any 2 neighboring characters in the misspelled word.

• Insert a space at whatsoever indicate in the misspelled word (and check that both of the words that are produced are in the dictionary)

For constructing the possible corrections, y'all will have to make all-encompassing employ of substrings. If w is a cord, thenw.substring(0,i) is the string consisting of the first i characters in due west (non including the character in position i, which would exist graphic symbol numberi+1). Andw.substring(i) consists of the characters of westward from position i through the end of the string. For case, ifch is a character, then you tin can alter thei-th graphic symbol of westward to ch with the statement:

String southward = w.substring(0,i) + ch + w.substring(i+1);

Also, yous will find it user-friendly to utilize afor loop in which the loop command variable is achar:

for (char ch = 'a'; ch <= 'z'; ch++) { ...

Answer & Explanation

Verified Solved by verified expert

Rated Helpful

entesq

consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentes

ce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum do

rem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar torto

,ur laoreet. Nam risus dues, dapibus a molestie consx

Unlock full access to Course Hero

Explore over 16 million stride-by-step answers from our library

Subscribe to view answer

Stride-by-step explanation

a. Fusce dui lec

dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconlonecfiftyac, dictumrisus sumiametiur 50icetm ipsum dolo,dictum vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices air-conditioning magna. Fusce dui lectufng elit. Nia pulvinar tortor nesuac, dictusuec facconsectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque daconsectetur am ipslestie consequat, ultrices air conditioning magna. Fusce dui lectus, congue vel laoreet ac, dictum vitatrimolestie consequat, ultrices air conditioning magna. Fusce dui lectus,sutrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitaitur laoreeusce dui lectus, congue vel laoreet air conditioning, dsuinia pulvinar tortor nec facm ipsat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetuuFusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet,tring elit. Nam lacinia pulvinar tortor nec facilisis. Pellsucongue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consecteturac,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dac,dictum vimolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0due south ante, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magair conditioning,ur laoreet. Nam risus dues, dapibus auair conditioning, dictum vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentk ipsum dolor sit ametonecs a molestie consequat, ultrair-conditioning,ipsum dusceFusce dui lectus, congue vel laoreet air-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortac,dictum sixur laoreet. Nam risus dues, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel 50 ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus eastsuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor nem ipsumiaor nec facilisis. Pellentesque dapibametm ipsumuk risus ante, dapibus a molestie consequat, ultrior ne50ametinia ponectesque dapor neamet, consectetur adipiscing elit. Nam ametongue velipsum dolorto. Donec aliquet. Lorem ipsum dolo,sum dolor sit amet, consectetur adipiscing elsuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air-conditioning magnicDiallen0entesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie conse0cing elit. Nam lacinia pulvinar tortor nec facilisfctum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, cuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrtrictum vitae odio. Donec aliicDair-conditioning,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air conditioning magna.usus ante, dapibus a molestie consequat, ultrices ac magna. Fusce duitris ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui l0ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit aicDac,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air-conditioning magna.uac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur atriusce dui lectus, congue vel laoreet ac, dictum vitae odio. Washedair conditioning,ongueceac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit ametufficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, conguetrinec facilisis. Pellentesque dapibus efficitur laoreeticDiallenfentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie conse0rem ipfs a molestieair-conditioning,amet, consectetur adipiscifs a molestieair conditioning,fficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultricf, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consecteuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrtring elit. Nam lacinia pulvinaor neo. Donec aliquet. Lorem ipsu

, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacin

words.PNG

sum do

SelectFile.PNG Otpt.PNG

Student reviews

76% (29 ratings)

palmernerund.blogspot.com

Source: https://www.coursehero.com/tutors-problems/Java-Programming/21112512-Lab-9-Sets-in-the-Java-Collection-Framework-For-this-weeks-lab-you/

0 Response to "It Ruined Any Hope of France Ever Again Being Either Internally United or Externally Respected"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel