Mindy Thornton May Be Best Known in Council Bluffs as a Former Martial Arts Instructor

Lab 9: Sets in the Java Collection Framework For this week's lab,...

Lab 9: Sets in the Coffee Collection Framework

For this week'due south lab, you will utilize 2 of the classes in the Java Collection Framework:HashSet andTreeSet. Yous will use these classes to implement a spell checker.

Set Methods

this lab, you will need to use some of the methods that are divers in the Set up interface. Recall that if gear up is a Ready, so the following methods are defined:

  • set.size() -- Returns the number of items in the set.
  • set.add(item) -- Adds the item to the prepare, if it is not already in that location.
  • set.contains(item) -- Check whether the ready contains the item.
  • fix.isEmpty() -- Cheque whether the prepare is empty.

You will besides need to exist able to traverse a ready, 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 language words, with one give-and-take on each line. You will look up words in this listing to bank check whether they are correctly spelled. To make the list easy to use, you can shop the words in a ready. Since there is no demand to have the words stored in order, you lot tin can utilize aHashSet for maximum efficiency.

Use a Scanner to read the file. You can create scanner,filein, for reading from a file with a statement such equally:

                     filein = new Scanner

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

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

while (filein.hasNext()) {

     Cord tk = filein.next();

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

}

(For the wordlist file, a token is but a give-and-take.)

Start your main program by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this plan,catechumen all words to lower case earlier putting them in the gear up. To make sure that you lot've read all the words, check the size of the prepare. (It should be 72875.) You could also use the contains method to bank check for the presence of some common give-and-take in the gear up.

Checking the Words in a File

Once you have the list of words in a set, it's easy to read the words from a file and check whether each word is in the set. Start by letting the user select a file. You can either let the user type the name of the file or yous tin use 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 render value is nothing.

         */

        static File getInputFileNameFromUser() {

           JFileChooser fileDialog = new JFileChooser();

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

           int choice = fileDialog.showOpenDialog(goose egg);

           if (option != JFileChooser.APPROVE_OPTION)

              return naught;

           else

              return fileDialog.getSelectedFile();

        }

Apply a Scanner to read the words from the selected file. In club to skip over any non-letter of the alphabet characters in the file, you tin can use the following command merely later creating the scanner (where in is the variable proper noun for the scanner):

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

(In this statement, "[^a-zA-Z]+" is a regular expression that matches whatever sequence of one or more non-letter characters. This substantially makes the scanner treat whatever not-alphabetic character the style information technology would ordinarily treat a space.)

You can then become through the file, read each give-and-take (converting it to lower case) and cheque whether the gear up contains the word. At this point, just print out whatever discussion that yous find that is non in the dictionary.

Providing a List of Possible Correct Spellings

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

static TreeSet corrections(Cord badWord, HashSet dictionary)

that creates and returns aTreeSet<Cord> containing variations onbadWord that are independent in the lexicon. In your main program, when you lot discover a word that is non in the set of legal words, pass that word to this method (along with the set). Accept the return value and output whatever words that information technology contains; these are the suggested correct spellings of the misspelled discussion. Hither, for example, is role of the output from a sample program when it was run with the HTML source of this folio as input:

html: (no suggestions)

cpsc: (no suggestions)

hashset: hash prepare

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, peel, pee, per, pie, poe, pore, prep, pres,

      casualty, pro, pry, pure, pyre, re

hasnext: has next

wordlist: discussion list

getinputfilenamefromuser: (no suggestions)

jfilechooser: (no suggestions)

filedialog: file dialog

setdialogtitle: (no suggestions)

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

     it, lint, mint, nit, pint, tint

Note that the program was written so that it volition not output the same misspelled word more than one time. (This is done by keeping a set of misspelled words that accept been output.) If thecorrections() method returns an empty set, the programme outputs the message "(no suggestions)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical guild with no repeats.

The possible corrections that the programme considers are as follows:

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

• Alter any alphabetic character in the misspelled word to whatever other letter.

• Insert any letter at any point in the misspelled word.

• Swap any ii neighboring characters in the misspelled give-and-take.

• Insert a space at any point in the misspelled word (and check that both of the words that are produced are in the lexicon)

For amalgam the possible corrections, you lot will take to brand all-encompassing utilise of substrings. If w is a string, and thenw.substring(0,i) is the string consisting of the first i characters in w (non including the character in position i, which would be character numberi+one). Andw.substring(i) consists of the characters of w from position i through the stop of the cord. For example, ifch is a character, then you can alter thei-th character of w to ch with the argument:

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

Also, you will observe it convenient to use 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 air conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum exercise

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

,ur laoreet. Nam risus ante, dapibus a molestie cons10

Unlock full access to Class Hero

Explore over sixteen million step-by-step answers from our library

Subscribe to view answer

Step-past-step explanation

a. Fusce dui lec

dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconfiftyonec50air conditioning, dictumrisus sumiametiur licetm ipsum dolo,dictum vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices ac 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 ac magna. Fusce dui lectus, congue vel laoreet air conditioning, 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 ac, dsuinia pulvinar tortor nec facm ipsat, ultrices air-conditioning magna. Fusce dui lectus, congue vel laoreet air-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit 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 al0s ante, dapibusicitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices ac magair-conditioning,ur laoreet. Nam risus ante, dapibus auac, dictum vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentyard ipsum dolor sit ametonecs a molestie consequat, ultrac,ipsum dusceFusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortac,dictum 6ur 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 dapibametthou ipsumum risus ante, dapibus a molestie consequat, ultrior nelametinia 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 dues, dapibus a molestie conse0cing elit. Nam lacinia pulvinar tortor nec facilisfctum vitae odio. Donec aliquet. Lorem ipsum dolor sit 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 ac 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. Washedac,ongueceac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down 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 ipfsouth a molestieair conditioning,amet, consectetur adipiscifs a molestieac,fficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultricf, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down 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)

0 Response to "Mindy Thornton May Be Best Known in Council Bluffs as a Former Martial Arts Instructor"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel