Lompat ke konten Lompat ke sidebar Lompat ke footer

Facets of a Diamond Pastor Stoles for Babies

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

Lab 9: Sets in the Java Drove Framework

For this week's lab, yous will use two of the classes in the Coffee Collection Framework:HashSet andTreeSet. Yous will use these classes to implement a spell checker.

Set Methods

this lab, y'all will demand to use some of the methods that are defined in the Set interface. Recall that if ready is a Ready, so the post-obit methods are defined:

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

You will also need to exist 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 give-and-take on each line. You volition look upwards words in this list to check whether they are correctly spelled. To make the list easy to utilize, you can store the words in a ready. Since there is no need to have the words stored in order, yous tin can employ 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 every bit:

                     filein = new Scanner

(new File("/classes/s09/cs225/words.txt"));

and that a file tin can be processed, token by token, in a loop such as:

while (filein.hasNext()) {

     String tk = filein.next();

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

}

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

Start your main program by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this programme,convert all words to lower example before putting them in the set. To make sure that you've read all the words, check the size of the set. (It should exist 72875.) You could likewise 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 gear up, it's easy to read the words from a file and check whether each give-and-take is in the set. Start by letting the user select a file. Y'all can either allow the user blazon the name of the file or you tin 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 null.

         */

        static File getInputFileNameFromUser() {

           JFileChooser fileDialog = new JFileChooser();

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

           int option = fileDialog.showOpenDialog(zero);

           if (choice != JFileChooser.APPROVE_OPTION)

              return null;

           else

              return fileDialog.getSelectedFile();

        }

Use a Scanner to read the words from the selected file. In social club to skip over whatever non-letter characters in the file, you lot tin can utilise the following control just after creating the scanner (where in is the variable proper noun 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 not-alphabetic character characters. This essentially makes the scanner care for whatsoever not-alphabetic character the way it would commonly 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 word. At this point, just impress out whatsoever word 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 too give 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 chief program, when you find a discussion that is not in the gear up of legal words, laissez passer that word to this method (along with the set). Have the render value and output any words that information technology contains; these are the suggested correct spellings of the misspelled word. Here, for case, is office of the output from a sample program when information technology was run with the HTML source of this folio as input:

html: (no suggestions)

cpsc: (no suggestions)

hashset: hash set

treeset: tree prepare

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 side by side

wordlist: word listing

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 so that it will not output the same misspelled word more than than once. (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 message "(no suggestions)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical social club with no repeats.

The possible corrections that the program considers are as follows:

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

• Alter whatever letter in the misspelled give-and-take to any other letter.

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

• Swap whatever two neighboring characters in the misspelled give-and-take.

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

For constructing the possible corrections, you will have to make extensive use of substrings. If westward is a string, thenw.substring(0,i) is the string consisting of the first i characters in west (non including the character in position i, which would be character numberi+1). Andw.substring(i) consists of the characters of w from position i through the stop of the string. For instance, ifch is a grapheme, then you can change thei-thursday character of w to ch with the statement:

String s = w.substring(0,i) + ch + w.substring(i+i);

Also, you will find information technology convenient to use afor loop in which the loop control variable is achar:

for (char ch = 'a'; ch <= 'z'; ch++) { ...

Reply & 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 down amet, consectetur adipiscing elit. Nam lacinia pulvinar torto

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

Unlock full access to Course Hero

Explore over 16 one thousand thousand stride-by-step answers from our library

Subscribe to view reply

Pace-by-step caption

a. Fusce dui lec

dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconloneclac, dictumrisus sumiametiur licetone thousand ipsum dolo,dictum vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices ac magna. Fusce dui lectufng elit. Nia pulvinar tortor nesuair conditioning, dictusuec 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 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 ac, 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 down amet, consecteturac,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dac,dictum vimolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0southward ante, dapibusicitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices ac magac,ur laoreet. Nam risus ante, dapibus auair-conditioning, dictum vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentm ipsum dolor sit ametonecs a molestie consequat, ultrair conditioning,ipsum dusceFusce dui lectus, congue vel laoreet air conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortair conditioning,dictum viur laoreet. Nam risus ante, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel 50 ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus esuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor nem ipsumiaor nec facilisis. Pellentesque dapibametm ipsumug 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 air conditioning 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 dues, 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 duitrisouth ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui l0ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit aicDac,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.uair-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetur atriusce dui lectus, congue vel laoreet ac, dictum vitae odio. Doneair conditioning,ongueceac, 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, conguetrinec facilisis. Pellentesque dapibus efficitur laoreeticDiallenfentesque dapibus efficitur laoreet. Nam risus dues, dapibus a molestie conse0rem ipfsouthward a molestieac,amet, consectetur adipiscifs a molestieac,fficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultricf, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down 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 practice

SelectFile.PNG Otpt.PNG

Student reviews

75% (28 ratings)

grossmestans1945.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/

Posting Komentar untuk "Facets of a Diamond Pastor Stoles for Babies"