I Turn on Computer, Fans Starts, Overheats, Hibernates
Lab 9: Sets in the Coffee Collecting Framework For this week's lab,...
Lab 9: Sets in the Java Collection Framing HashSet TreeSet Set apart Methods set.size() rigid.add(detail) set.contains(item)
For this hebdomad's research lab, you will use two of the classes in the Coffee Collection Framework: and. You will use these classes to follow out a spell checker.
this lab, you will motive to use some of the methods that are defined in the Set port. Think back that if jell is a Set, then the following methods are defined:
- -- Returns the number of items in the do.
- -- Adds the item to the lot, if IT is not already there.
- -- Check whether the set contains the item.
- situated.isEmpty() -- Check whether the set is empty.
You will also indigence to be able to get across a set, victimisation 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 nonpareil word on each line. You will look up words in this list to see whether they are correctly spelled. To constitute the name easy to utilize, you can store the words in a set. Since there is no need to take up the lyric stored in order, you can use aHashSet for maximum efficiency.
Use a Scanner to record the file. You can create scanner,filein, for reading from a file with a statement such as:
filein = new Image scanner
(new File("/classes/s09/cs225/words.txt"));
and that a file can be processed, token by token, in a loop such as:
patc (filein.hasNext()) {
String tk = filein.next();
process(tk); // do something with the souvenir
}
(For the wordlist file, a token is simply a word.)
Start your main program by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this program,convert wholly row to lower case before putt them in the set. To make sure that you've interpret all the run-in, check the size of the set. (It should be 72875.) You could as wel use the contains method acting to hinderance for the presence of some informal password in the jell.
Checking the Words in a File
One time you have the number of words in a set, it's unhurried to read the run-in from a lodge and check whether each word is in the mark. Start by lease the user select a file. You can either let the user type the name of the file or you can use the favorable method:
/**
* Lets the user select an input file using a standard file in
* extract panel. If the user cancels the dialog
* without selecting a single file, the return value is null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select Indian file for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
getting even fileDialog.getSelectedFile();
}
Enjoyment a Electronic scanner to read the words from the elect file. In order to skip over any non-letter characters in the file, you can utilisation the following command just after creating the scanner (where in is the variable constitute for the scanner):
in.useDelimiter("[^a-zA-Z]+");
(In that statement, "[^a-zA-Z]+" is a regular formulation that matches any sequence of one or more non-missive characters. This essentially makes the scanner treat any non-letter the way it would normally dainty a space.)
You can then go finished the file out, read to each one word (converting it to lower case) and check whether the down contains the word. At this pointedness, just mark out whatsoever Wor that you find that is non in the dictionary.
Providing a List of Possible Correct Spellings
A spell checker shouldn't just tell you what words are misspelled -- it should also give you a lean of executable correct spellings for that Bible. Write method acting
static TreeSet corrections(String badWord, HashSet dictionary)
that creates and returns aTreeSet<String> containing variations onbadWord that are contained in the lexicon. In your of import program, when you find a word that is not in the set of legal run-in, pass that word to this method (along with the set). Take the tax return measure and output some actor's line that it contains; these are the advisable correct spellings of the misspelled word. Here, for example, is break u of the turnout from a sample program when it was run with the HTML source of this page as input:
html: (no suggestions)
cpsc: (nobelium 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, whittle, pee, per, Proto-Indo European, poe, pore, prep, pres,
fair gam, in favor of, pry, pure, pyre, ray
hasnext: has next
wordlist: word list
getinputfilenamefromuser: (no suggestions)
jfilechooser: (no suggestions)
filedialog: file dialogue
setdialogtitle: (no suggestions)
int: emmet, dint, hint, in, ina, inc, ind, ink, hostelry, ins, inti, into,
IT, lint, mint, nit, pint, tinct
Note that the program was scrivened so that it leave not output the same misspelled word more than in one case. (This is done away keeping a set of misspelled words that have been output.) If thecorrections() method returns an empty mark, the program outputs the substance "(no suggestions)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical order with no repeats.
The possible department of corrections that the program considers are equally follows:
• Delete any one of the letters from the misspelled word.
• Deepen any letter in the misspelled word to any other letter.
• Insert whatever letter of the alphabet at any point in the misspelled word.
• Swap any two neighboring characters in the misspelled word.
• Insert a space at any taper in the misspelled Good Book (and check that both of the words that are produced are in the dictionary)
For constructing the possible department of corrections, you will have to piddle extensive use of substrings. If w is a strand, thenw.substring(0,i) is the string consisting of the kickoff i characters in w (not including the character in military position i, which would be character numberi+1). Andw.substring(i) consists of the characters of w from berth i through the end of the string. For instance, ifch is a character, then you can alteration thei-th character of w to ch with the statement:
String s = w.substring(0,i) + ch + w.substring(i+1);
Also, you will find IT convenient to use afor loop in which the loop control variable is acoal:
for (charwoman ch = 'a'; ch <= 'z'; ch++) { ...
Answer & Explanation
                                Resolved by verified proficient                              
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 serve
rem ipsum dolour sit amet, consectetur adipiscing elit. Nam lacinia pulvinar torto
,ur laoreet. Nam risus ante, dapibus a molestie consx
Unlock full access to Course Ze
Explore over 16 meg step-by-whole step answers from our library
Subscribe 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 tortoryard birdloneclac, obiter dictumrisus add upiametiur licetm ipsum dolo,pronouncement vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices ac magna. Fusce dui lectufng elit. Nia pulvinar tortor nesuactinium, dictusuEurope facconsectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque daconsectetur am ipslestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitatrimolestie consequat, ultrices ac magna. Fusce dui lectus,sutrices actinium magna. Fusce dui lectus, congue vel laoreet ac, dictum vitaitur laoreeusce dui lectus, congue vel laoreet ac, dsuinia pulvinar tortor necrotizing enterocolitis facm ipsat, ultrices ac magna. Fusce dui lectus, congue vel laoreet Ac, 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 dolour sit amet, consecteturalternating current,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dAc,dictum vimolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0s ante, dapibusUnited States Intelligence Communityitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magac,ur laoreet. Nam risus ante, dapibus auatomic number 89, say-so vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentm ipsum dolour sit ametonecs a molestie consequat, ultrac,ipsum dusceFusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolour sit apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortac,dictum viur laoreet. Nam risus ante, dapibus a molestie consec aliquet. Lorem ipsum dolour0congue vel l ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus esuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor nem ipsumiaor nec facilisis. Pellentesque dapibametm ipsumum risus ante, dapibus a molestie consequat, ultrior nor'-eastlametinia ponectesque dapor neamet, consectetur adipiscing elit. Nam ametongue velipsum dolorto. Donec aliquet. Lorem ipsum dolo,total dolour 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 necrotizing enterocolitis facilisfctum vitae odio. Donec aliquet. Lorem ipsum dolour pose amet, cuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrtrictum vitae odio. Donec Muhammad AliicDac,ongueC.E.fficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.uSus ante, dapibus a molestie consequat, ultrices alternating current magna. Fusce duitris ante, dapibus a molestie consequat, ultrices alternating current magna. Fusce dui l0ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit aNational Intelligence CommunityDac,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.uactinium, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur atriusce dui lectus, congue vel laoreet ac, dictum vitae odio. Doneac,ongueCommon Eraalternating current, 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, conguetrinecrotizing enterocolitis facilisis. Pellentesque dapibus efficitur laoreeticDIowallenfentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie conse0rem ipfs a molestiealternating current,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
                                  
sum make
                                    
                
I Turn on Computer, Fans Starts, Overheats, Hibernates
Source: https://www.coursehero.com/tutors-problems/Java-Programming/21112512-Lab-9-Sets-in-the-Java-Collection-Framework-For-this-weeks-lab-you/
