[Solved] Lab 9: Sets in the Java Collection Framework For this week’s lab,… | Course Hero

Asked by Jaydeep89

Lab 9: Sets in the Java Collection Framework For this week’s lab,…

Lab 9: Sets in the Java Collection Framework

For this workweek ‘s lab, you will use two of the classes in the Java Collection Framework : HashSet and TreeSet. You will use these classes to implement a spell check.
Set Methods this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the follow methods are defined :

  • set.size() — Returns the number of items in the set.
  • set.add(item) — Adds the item to the set, if it is not already there.
  • set.contains(item) — Check whether the set contains the item.
  • set.isEmpty() — Check whether the set is empty.

You will besides need to be able to traverse a fit, using either an iterator or a for-each cringle.
Reading a Dictionary The file words.txt ( in the code directory ) contains a list of English words, with one word on each pipeline. You will look up words in this list to check whether they are correctly spelled. To make the number easy to use, you can store the words in a set. Since there is no motivation to have the words stored in order, you can use a HashSet for maximum efficiency.
Use a Scanner to read the file. You can create scanner, filein, for reading from a file with a statement such as :
filein = new Scanner ( raw File ( “ /classes/s09/cs225/words.txt ” ) ) ;
and that a file can be processed, token by keepsake, in a coil such as :
while ( filein.hasNext ( ) ) { String tk = filein.next ( ) ; procedure ( tk ) ; // do something with the token }
( For the wordlist file, a nominal is just a word. )
Start your main program by reading the words from words.txt and storing them in a HashSet . For the purposes of this program, convert all words to lower case before putting them in the set. To make sure that you’ve read all the words, check the size of the set. (It should be 72875.) You could also use the contains method to check for the presence of some common word in the set.
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 bible is in the located. Start by letting the exploiter select a file. You can either let the exploiter type the name of the file or you can use the follow method :
/ * * * Lets the drug user select an input signal file using a standard file * survival dialogue box. If the exploiter cancels the dialogue * without selecting a file, the reelect value is null. * / static File getInputFileNameFromUser ( ) { JFileChooser fileDialog = new JFileChooser ( ) ; fileDialog.setDialogTitle ( “ Select File for Input ” ) ; int option = fileDialog.showOpenDialog ( nothing ) ; if ( choice ! = JFileChooser.APPROVE_OPTION ) rejoinder null ; else render fileDialog.getSelectedFile ( ) ; }
Use a Scanner to read the words from the selected file. In order to skip over any non-letter characters in the file, you can use the follow instruction just after creating the scanner ( where in is the variable name for the scanner ) :

in.useDelimiter ( “ [ ^a-zA-Z ] + ” ) ;
( In this argument, “ [ ^a-zA-Z ] + ” is a regular formulation that matches any sequence of one or more non-letter characters. This basically makes the scanner process any non-letter the way it would normally treat a space. ) You can then go through the file, read each word ( converting it to lower case ) and check whether the set contains the news. At this point, barely print out any word that you find that is not in the dictionary.
Providing a List of Possible Correct Spellings A enchantment checker should n’t just tell you what words are misspelled — it should besides give you a list of possible chastise spellings for that news. Write method
static TreeSet corrections ( String badWord, HashSet dictionary )
that creates and returns a TreeSet  containing variations on badWord that are contained in the dictionary. In your main program, when you find a word that is not in the set of legal words, pass that word 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 word. Here, for example, is part of the output from a sample program when it was run with the HTML source of this page as input:
hypertext markup language : ( no suggestions ) cpsc : ( no suggestions ) hashset : hashish determine treeset : tree set curriculum vitae : c, five isempty : is vacate href : referee txt : thematic apperception test, tet, text, nipple, tot, tsk filein : file in pre : are, ere, wrath, ore, skin, make, per, proto-indo european, poe, pore, homework, pres, raven, pro, pry, pure, pyre, re hasnext : has future wordlist : word tilt getinputfilenamefromuser : ( no suggestions ) jfilechooser : ( no suggestions ) filedialog : file dialogue setdialogtitle : ( no suggestions ) int : ant, dint, trace, in, ina, iraqi national congress, ind, ink, hostel, ins, inti, into, it, lint, mint, nit, pint, tint
note that the program was written therefore that it will not output signal the same misspelled son more than once. ( This is done by keeping a set of misspell words that have been output. ) If the corrections ( ) method acting returns an empty set, the program outputs the message “ ( no suggestions ) ”. Since the corrections are stored in a tree set, they are mechanically printed out in alphabetic order with no repeats.
The potential corrections that the program considers are as follows : • Delete any one of the letters from the misspelled bible. • Change any letter in the misspell password to any other letter. • Insert any letter at any point in the misspell news. • Swap any two neighbor characters in the misspell parole. • Insert a space at any point in the misspell word ( and check that both of the words that are produced are in the dictionary )
For constructing the possible corrections, you will have to make across-the-board consumption of substrings. If watt is a string, then w.substring ( 0, iodine ) is the string consist of the first one characters in west ( not including the fictional character in position i, which would be character number i+1 ). And w.substring ( one ) consists of the characters of w from status i through the end of the string. For example, if ch is a fictional character, then you can change the i-th character of tungsten to ch with the statement :
String south = w.substring ( 0, iodine ) + ch + w.substring ( i+1 ) ;

besides, you will find it convenient to use a for loop in which the cringle command variable is a char :
for ( char ch = ‘ a ‘ ; ch < = ' omega ' ; ch++ ) { ...

source : https://dangthanhvinh.com
Category : Blog

Trả lời

Email của bạn sẽ không được hiển thị công khai.