Mi Bay City Putts Hardware Canning Jars

Lab 9: Sets in the Coffee Drove Framework For this week's lab,...

Lab 9: Sets in the Java Collection Framework

For this week's lab, y'all will use two of the classes in the Java Collection Framework:HashSet andTreeSet. You will use these classes to implement a spell checker.

Set Methods

this lab, you volition need to employ some of the methods that are defined in the Ready interface. Call up that if ready is a Gear up, then the post-obit methods are defined:

  • set.size() -- Returns the number of items in the set.
  • set.add(item) -- Adds the item to the set, if information technology is not already there.
  • set.contains(item) -- Check whether the set contains the particular.
  • set.isEmpty() -- Check whether the ready is empty.

You will also need 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 listing of English words, with one word on each line. Y'all will look upward words in this listing to check whether they are correctly spelled. To make the list easy to use, you can store the words in a set. Since there is no demand to accept the words stored in order, y'all can employ aHashSet for maximum efficiency.

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

                     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.adjacent();

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

}

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

Starting time 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 gear up. To make certain that you lot've read all the words, check the size of the ready. (Information technology should be 72875.) Y'all could too use the contains method to cheque for the presence of some common give-and-take in the set.

Checking the Words in a File

Once yous take the listing of words in a fix, information technology's like shooting fish in a barrel 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 tin can either let the user type the name of the file or you tin can use the post-obit method:

        /**

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

         * pick 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 option = fileDialog.showOpenDialog(cypher);

           if (choice != JFileChooser.APPROVE_OPTION)

              render null;

           else

              return fileDialog.getSelectedFile();

        }

Use a Scanner to read the words from the selected file. In gild to skip over any non-letter characters in the file, you tin can apply the post-obit command just afterward 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 whatsoever sequence of one or more not-letter characters. This essentially makes the scanner treat any non-letter the way it would ordinarily care for a space.)

You can and then go through the file, read each word (converting it to lower case) and cheque whether the fix contains the word. At this indicate, just impress out any word that y'all find that is not in the lexicon.

Providing a List of Possible Correct Spellings

A spell checker shouldn't just tell you what words are misspelled -- it should too give you 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 independent in the lexicon. In your chief programme, when you detect a word that is not in the gear up of legal words, pass that word to this method (along with the set). Take the return value and output any words that information technology contains; these are the suggested right spellings of the misspelled word. Here, for instance, is part of the output from a sample program when it was run with the HTML source of this page every bit 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 adjacent

wordlist: discussion listing

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,

     it, lint, mint, nit, pint, tint

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

The possible corrections that the plan considers are as follows:

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

• Change any letter of the alphabet in the misspelled word to any other letter.

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

• Swap whatever two neighboring characters in the misspelled word.

• Insert a infinite at any signal in the misspelled word (and check that both of the words that are produced are in the dictionary)

For constructing the possible corrections, you will take to make extensive use of substrings. If w is a cord, sow.substring(0,i) is the cord consisting of the first i characters in w (non including the character in position i, which would be grapheme numberi+1). Andw.substring(i) consists of the characters of w from position i through the end of the cord. For case, ifch is a character, then you tin change thei-thursday character of w to ch with the statement:

Cord s = west.substring(0,i) + ch + due west.substring(i+1);

Too, you will find 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 past 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 amet, consectetur adipiscing elit. Nam lacinia pulvinar torto

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

Unlock full access to Course Hero

Explore over sixteen 1000000 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 tortorconfiftyoneclac, dictumrisus sumiametiur licetm 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 agrand ipslestie consequat, ultrices air conditioning magna. Fusce dui lectus, congue vel laoreet ac, dictum vitatrimolestie consequat, ultrices ac magna. Fusce dui lectus,sutrices air-conditioning magna. Fusce dui lectus, congue vel laoreet air-conditioning, 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 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 dair-conditioning,dictum 6molestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0south ante, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magazineac,ur laoreet. Nam risus ante, dapibus auair-conditioning, dictum vitae odio. Donec afinia pulvinar tortor nec facair conditioning,ipsum dentm ipsum dolor sit down ametonecs a molestie consequat, ultrair conditioning,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 viur laoreet. Nam risus ante, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel l ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus eastwardsuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor nechiliad ipsumiaor nec facilisis. Pellentesque dapibametm ipsumum 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 ac 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 aliicDac,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.usus ante, dapibus a molestie consequat, ultrices air-conditioning magna. Fusce duitris ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui l0ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit aicDair conditioning,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.uac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetur atriusce dui lectus, congue vel laoreet ac, dictum vitae odio. Washedac,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 ipfdue south a molestieac,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

75% (28 ratings)

0 Response to "Mi Bay City Putts Hardware Canning Jars"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel