Make the Golf Course Green Again Ronald Frump

Lab ix: Sets in the Java Collection Framework For this week's lab,...

Lab 9: Sets in the Coffee Collection Framework

For this week's lab, you will use ii of the classes in the Java Collection Framework:HashSet andTreeSet. You volition use these classes to implement a spell checker.

Set Methods

this lab, you lot will need to use some of the methods that are defined in the Gear up interface. Recollect that if set is a Set, and so the following methods are defined:

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

You will also need to be able to traverse a fix, using either an iterator or a for-each loop.

Reading a Dictionary

The file words.txt (in the lawmaking directory) contains a list of English words, with i word on each line. You lot will expect up words in this listing to cheque whether they are correctly spelled. To brand the list piece of cake to utilize, you tin can shop the words in a set. Since in that location is no demand to have the words stored in gild, you lot can use aHashSet for maximum efficiency.

Employ a Scanner to read the file. You can create scanner,filein, for reading from a file with a argument such as:

                     filein = new Scanner

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

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

while (filein.hasNext()) {

     String tk = filein.next();

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

}

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

Starting time your main plan past reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this plan,catechumen all words to lower case before putting them in the set up. To make sure that you've read all the words, check the size of the set. (It should be 72875.) Yous could too use the contains method to check for the presence of some mutual give-and-take in the set.

Checking the Words in a File

Once you have the list of words in a fix, it's like shooting fish in a barrel to read the words from a file and check whether each word is in the set. Offset by letting the user select a file. You lot tin can either let the user type the name of the file or yous can employ the following method:

        /**

         * Lets the user select an input file using a standard file

         * pick dialog box. If the user cancels the dialog

         * without selecting a file, the return value is null.

         */

        static File getInputFileNameFromUser() {

           JFileChooser fileDialog = new JFileChooser();

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

           int option = fileDialog.showOpenDialog(nada);

           if (option != JFileChooser.APPROVE_OPTION)

              return null;

           else

              return fileDialog.getSelectedFile();

        }

Use a Scanner to read the words from the selected file. In order to skip over any not-letter of the alphabet characters in the file, you can use the post-obit command merely 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 non-alphabetic character characters. This essentially makes the scanner treat any not-letter of the alphabet the way it would usually treat a space.)

Y'all tin can so get through the file, read each word (converting it to lower instance) and check whether the set contains the word. At this betoken, just print out whatever give-and-take that you lot find that is not in the dictionary.

Providing a List of Possible Correct Spellings

A spell checker shouldn't simply tell yous what words are misspelled -- information technology should also give you a list of possible correct spellings for that discussion. Write method

static TreeSet corrections(String badWord, HashSet dictionary)

that creates and returns aTreeSet<Cord> containing variations onbadWord that are independent in the dictionary. In your main program, when you find a word that is not in the set up of legal words, pass that give-and-take to this method (along with the prepare). Take the return value and output whatever 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 programme 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: discussion listing

getinputfilenamefromuser: (no suggestions)

jfilechooser: (no suggestions)

filedialog: file dialog

setdialogtitle: (no suggestions)

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

     information technology, lint, mint, nit, pint, tint

Notation that the plan was written so that it volition non output the same misspelled word more than once. (This is washed past keeping a ready of misspelled words that take been output.) If thecorrections() method returns an empty set, the program outputs the bulletin "(no suggestions)". Since the corrections are stored in a tree fix, they are automatically printed out in alphabetical social club with no repeats.

The possible corrections that the program considers are equally follows:

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

• Change whatsoever letter in the misspelled word to whatever other letter of the alphabet.

• Insert any letter at whatsoever point in the misspelled discussion.

• Swap any 2 neighboring characters in the misspelled discussion.

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

For constructing the possible corrections, yous will accept to make all-encompassing utilise of substrings. If westward is a cord, thenw.substring(0,i) is the string consisting of the first i characters in westward (not including the grapheme in position i, which would exist character numberi+i). Andw.substring(i) consists of the characters of w from position i through the stop of the string. For example, ifch is a character, and then yous tin can change thei-thursday character of west to ch with the statement:

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

Also, you will find it convenient to employ afor loop in which the loop control variable is achar:

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

Answer & Explanation

Verified Solved by verified good

Rated Helpful

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 do

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 Course Hero

Explore over 16 meg step-past-step answers from our library

Subscribe to view respond

Footstep-by-step caption

a. Fusce dui lec

dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconloneclair conditioning, dictumrisus sumiametiur licetone thousand ipsum dolo,dictum vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices air conditioning 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 air-conditioning magna. Fusce dui lectus, congue vel laoreet air-conditioning, dictum vitatrimolestie consequat, ultrices ac magna. Fusce dui lectus,sutrices air conditioning magna. Fusce dui lectus, congue vel laoreet ac, dictum vitaitur laoreeusce dui lectus, congue vel laoreet air-conditioning, 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 ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet,tring elit. Nam lacinia pulvinar tortor nec facilisis. Pellsucongue vel laoreet air-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consecteturac,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dair conditioning,dictum 6molestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0due south dues, dapibusicitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices ac magair-conditioning,ur laoreet. Nam risus ante, dapibus auac, dictum vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentm ipsum dolor sit ametonecsouthward a molestie consequat, ultrac,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 tortac,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 nem 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 dues, 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 duitridue south dues, dapibus a molestie consequat, ultrices air-conditioning magna. Fusce dui 500ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down aicDair-conditioning,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.uac, 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 dues, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, conguetrinec facilisis. Pellentesque dapibus efficitur laoreeticDiallenfentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie conse0rem ipfsouth a molestieair conditioning,amet, consectetur adipiscifdue south 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 down amet, consectetur adipiscing elit. Nam lacin

words.PNG

sum practice

SelectFile.PNG Otpt.PNG

Student reviews

76% (29 ratings)

joycegoodditin.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 "Make the Golf Course Green Again Ronald Frump"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel