Lawmaker Keep Baby Comfortable While Mother Decides to Abort
Lab 9: Sets in the Java Drove Framework For this week'southward lab,...
Lab 9: Sets in the Coffee Collection Framework
For this week's lab, y'all will use 2 of the classes in the Java Drove Framework:HashSet andTreeSet. Yous will utilise these classes to implement a spell checker.
Fix Methods
this lab, you lot will demand to utilise some of the methods that are defined in the Set interface. Remember that if ready is a Set, then the following methods are divers:
- set.size() -- Returns the number of items in the set.
- prepare.add together(item) -- Adds the item to the set, if information technology is not already there.
- prepare.contains(item) -- Check whether the ready contains the item.
- set up.isEmpty() -- Cheque whether the set is empty.
You volition as well need to be able to traverse a set, using either an iterator or a for-each loop.
Reading a Lexicon
The file words.txt (in the code directory) contains a list of English words, with ane give-and-take on each line. You volition look up words in this list to check whether they are correctly spelled. To make the list easy to use, you lot can store the words in a set. Since there is no need to accept the words stored in guild, you can utilize aHashSet for maximum efficiency.
Utilise a Scanner to read the file. You 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 past token, in a loop such as:
while (filein.hasNext()) {
String tk = filein.side by side();
process(tk); // practise something with the token
}
(For the wordlist file, a token is simply a discussion.)
Start your chief 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 set up. To make certain that you've read all the words, check the size of the set up. (It should exist 72875.) You could also use the contains method to check for the presence of some common give-and-take in the set.
Checking the Words in a File
Once you accept the list of words in a prepare, information technology's easy to read the words from a file and check whether each word is in the gear up. Starting time by letting the user select a file. Y'all tin can either let the user type the proper noun of the file or y'all can 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 cypher.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (selection != JFileChooser.APPROVE_OPTION)
return nix;
else
return fileDialog.getSelectedFile();
}
Utilise a Scanner to read the words from the selected file. In order to skip over any not-letter characters in the file, you tin use the following control 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 whatever sequence of one or more non-alphabetic character characters. This substantially makes the scanner treat whatever non-letter the way it would ordinarily treat a space.)
Yous can then become through the file, read each word (converting it to lower case) and bank check whether the set contains the word. At this betoken, just print out any give-and-take that you find that is not in the dictionary.
Providing a List of Possible Correct Spellings
A spell checker shouldn't simply tell y'all what words are misspelled -- it should also requite you a list of possible right spellings for that word. Write method
static TreeSet corrections(String badWord, HashSet lexicon)
that creates and returns aTreeSet<String> containing variations onbadWord that are contained in the dictionary. In your main program, when you find a give-and-take that is not in the set of legal words, laissez passer that word to this method (along with the set up). Take the return value and output whatever words that information technology contains; these are the suggested correct spellings of the misspelled word. Here, for case, is part of the output from a sample program when it was run with the HTML source of this page equally input:
html: (no suggestions)
cpsc: (no suggestions)
hashset: hash set
treeset: tree set up
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,
casualty, pro, pry, pure, pyre, re
hasnext: has side by side
wordlist: word 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,
information technology, lint, mint, nit, pint, tint
Notation that the program was written and then that it will not output the same misspelled word more than once. (This is done past keeping a set of misspelled words that take been output.) If thecorrections() method returns an empty prepare, the program outputs the message "(no suggestions)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical gild with no repeats.
The possible corrections that the plan considers are as follows:
• Delete whatever one of the letters from the misspelled word.
• Change any letter in the misspelled word to any other alphabetic character.
• Insert any letter at whatsoever point in the misspelled word.
• Swap any 2 neighboring characters in the misspelled give-and-take.
• Insert a space at whatsoever signal in the misspelled word (and bank check that both of the words that are produced are in the dictionary)
For constructing the possible corrections, y'all will have to brand extensive employ of substrings. If w is a cord, thenw.substring(0,i) is the string consisting of the commencement i characters in w (not including the grapheme in position i, which would exist character numberi+1). Andwest.substring(i) consists of the characters of west from position i through the cease of the string. For example, ifch is a grapheme, then yous can modify thei-th character of w to ch with the statement:
Cord s = w.substring(0,i) + ch + w.substring(i+i);
Likewise, yous 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
Solved by verified expert
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 practice
rem ipsum dolor sit down amet, consectetur adipiscing elit. Nam lacinia pulvinar torto
,ur laoreet. Nam risus ante, dapibus a molestie consx
Unlock full access to Class Hero
Explore over 16 million footstep-by-footstep answers from our library
Subscribe to view answer
Step-by-step caption
a. Fusce dui lec
dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconloneclair conditioning, dictumrisus sumiametiur licetk 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 air-conditioning, 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 half dozenmolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0s ante, dapibusicitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices air conditioning magac,ur laoreet. Nam risus ante, dapibus auair-conditioning, dictum vitae odio. Donec afinia pulvinar tortor nec facair conditioning,ipsum dentg 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 tortair-conditioning,dictum 6ur laoreet. Nam risus dues, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel l ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus due eastsuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor nem ipsumiaor nec facilisis. Pellentesque dapibametm ipsumugrand 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 down 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 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 air-conditioning magna. Fusce dui l0air-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down aicDac,onguecefficituriafficitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices ac magna.uair conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur atriusce dui lectus, congue vel laoreet air-conditioning, dictum vitae odio. Doneac,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 dues, dapibus a molestie conse0rem ipfs a molestieair-conditioning,amet, consectetur adipiscifs a molestieac,fficitur laoreet. Nam risus dues, 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 do
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 "Lawmaker Keep Baby Comfortable While Mother Decides to Abort"
Publicar un comentario