The Best Seat in the House a Cock Sparrer Story

Lab 9: Sets in the Java Drove Framework HashSet TreeSet Set Methods set.size() fix.add together(detail) set.contains(item)

For this week'due south lab, yous will use ii of the classes in the Java Drove Framework: and. You lot volition employ these classes to implement a spell checker.

this lab, you will demand to utilise some of the methods that are defined in the Ready interface. Recall that if set up is a Fix, then the post-obit methods are divers:

  •  -- Returns the number of items in the fix.
  •  -- Adds the detail to the prepare, if it is not already in that location.
  •  -- Check whether the set contains the item.
  • gear up.isEmpty() -- Check whether the set 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 list of English words, with one word on each line. You will look upward words in this list to cheque whether they are correctly spelled. To make the list like shooting fish in a barrel to employ, yous can store the words in a set. Since there is no demand to have the words stored in social club, you can utilise aHashSet for maximum efficiency.

Employ a Scanner to read the file. You tin 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 be processed, token by token, in a loop such as:

while (filein.hasNext()) {

     String tk = filein.next();

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

}

(For the wordlist file, a token is merely a discussion.)

Kickoff your main programme by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this plan,convert all words to lower case earlier putting them in the prepare. To brand sure that you've read all the words, check the size of the set. (It should be 72875.) Yous could likewise apply the contains method to check for the presence of some mutual give-and-take in the set.

Checking the Words in a File

Once yous take the list of words in a ready, it's easy to read the words from a file and check whether each word is in the ready. Start by letting the user select a file. You tin can either let the user type the proper name of the file or you lot tin utilize the post-obit 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 zero.

         */

        static File getInputFileNameFromUser() {

           JFileChooser fileDialog = new JFileChooser();

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

           int pick = fileDialog.showOpenDialog(cipher);

           if (option != JFileChooser.APPROVE_OPTION)

              render zip;

           else

              return fileDialog.getSelectedFile();

        }

Use a Scanner to read the words from the selected file. In order to skip over whatever non-alphabetic character characters in the file, yous can utilize the following control but 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 than not-letter characters. This substantially makes the scanner care for whatsoever non-letter the fashion it would ordinarily treat a space.)

Y'all can then become through the file, read each word (converting information technology to lower example) and check whether the ready contains the give-and-take. At this bespeak, just print out whatever give-and-take that yous discover that is not in the dictionary.

Providing a List of Possible Correct Spellings

A spell checker shouldn't only tell you what words are misspelled -- it should too give you a listing of possible right 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 lexicon. In your main program, when y'all find a word that is not in the prepare of legal words, pass that give-and-take to this method (along with the set). Take the return value and output any words that it contains; these are the suggested correct spellings of the misspelled discussion. Here, for example, is office 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 ready

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: give-and-take list

getinputfilenamefromuser: (no suggestions)

jfilechooser: (no suggestions)

filedialog: file dialog

setdialogtitle: (no suggestions)

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

     it, lint, mint, nit, pint, tint

Notation that the program was written then that it will not output the aforementioned misspelled give-and-take more than than once. (This is washed by keeping a set of misspelled words that have been output.) If thecorrections() method returns an empty set, the program outputs the message "(no suggestions)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical order with no repeats.

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

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

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

• Insert any letter at whatsoever betoken in the misspelled word.

• Swap any two neighboring characters in the misspelled word.

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

For constructing the possible corrections, you volition have to make extensive use of substrings. If westward is a string, thenw.substring(0,i) is the cord consisting of the start i characters in westward (not including the graphic symbol in position i, which would be character numberi+1). Andw.substring(i) consists of the characters of west from position i through the cease of the string. For example, ifch is a character, and so you can alter thei-th grapheme of w to ch with the argument:

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

Also, you will find it user-friendly to use afor loop in which the loop control 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 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 Form Hero

Explore over xvi one thousand thousand stride-by-pace answers from our library

Subscribe to view answer

Step-by-step explanation

a. Fusce dui lec

dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconloneclair conditioning, dictumrisus sumiametiur licetyard 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 achiliad ipslestie consequat, ultrices ac 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 air conditioning, dictum vitaitur laoreeusce dui lectus, congue vel laoreet air-conditioning, dsuinia pulvinar tortor nec facthou 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 down amet, consecteturair conditioning,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dac,dictum half-dozenmolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0s ante, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air conditioning magazineair conditioning,ur laoreet. Nam risus ante, dapibus auac, dictum vitae odio. Donec afinia pulvinar tortor nec facair conditioning,ipsum dentm ipsum dolor sit ametonecdue south 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 tortair conditioning,dictum half dozenur laoreet. Nam risus dues, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel l ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus esuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor negrand ipsumiaor nec facilisis. Pellentesque dapibametm 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 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 amet, cuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrtrictum vitae odio. Donec aliicDac,onguecefficituriafficitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices air conditioning magna.usus ante, dapibus a molestie consequat, ultrices ac magna. Fusce duitridue south ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui l0air conditioning, 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. Doneac,ongueceac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit ametufficitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, conguetrinec facilisis. Pellentesque dapibus efficitur laoreeticDiallenfentesque dapibus efficitur laoreet. Nam risus dues, dapibus a molestie conse0rem ipfs 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 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)

yohousprie.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 "The Best Seat in the House a Cock Sparrer Story"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel