Skip to content Skip to sidebar Skip to footer

Once and Again Consignment Sabina Ohio

Lab 9: Sets in the Java Collection Framework For this week'south lab,...

Lab 9: Sets in the Java Drove Framework

For this calendar week's lab, you will apply 2 of the classes in the Java Collection Framework:HashSet andTreeSet. You will utilize these classes to implement a spell checker.

Ready Methods

this lab, you will demand to utilise some of the methods that are defined in the Gear up interface. Recall that if set is a Gear up, so the following 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) -- Bank check whether the set contains the item.
  • set up.isEmpty() -- Check whether the set is empty.

You will likewise need to be 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 language words, with one word on each line. You will look up words in this list to check whether they are correctly spelled. To make the list piece of cake to use, you tin can store the words in a set up. Since in that location is no need to accept the words stored in society, you lot can utilise 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 equally:

                     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.side by side();

     process(tk); // exercise something with the token

}

(For the wordlist file, a token is merely a discussion.)

Start your chief plan past reading the words from words.txt and storing them in aHashSet<Cord>. For the purposes of this program,convert all words to lower example earlier putting them in the set. To make sure that yous've read all the words, bank check the size of the set. (Information technology should be 72875.) Yous could also employ the contains method to bank 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 word is in the set. Start past letting the user select a file. You tin can either let the user blazon the proper name of the file or yous can use the post-obit 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 return value is goose egg.

         */

        static File getInputFileNameFromUser() {

           JFileChooser fileDialog = new JFileChooser();

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

           int choice = fileDialog.showOpenDialog(null);

           if (option != JFileChooser.APPROVE_OPTION)

              render null;

           else

              render fileDialog.getSelectedFile();

        }

Use a Scanner to read the words from the selected file. In guild to skip over whatsoever non-letter characters in the file, you can use the following command just subsequently 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-letter of the alphabet characters. This essentially makes the scanner care for any non-alphabetic character the way information technology would ordinarily care for a space.)

Y'all can so get through the file, read each word (converting it to lower case) and check whether the gear up contains the discussion. At this point, merely print out whatsoever word that you detect that is not in the lexicon.

Providing a List of Possible Right Spellings

A spell checker shouldn't merely tell you what words are misspelled -- it should also give you a list of possible right spellings for that word. Write method

static TreeSet corrections(String badWord, HashSet dictionary)

that creates and returns aTreeSet<String> containing variations onbadWord that are contained in the lexicon. In your main plan, when you find a give-and-take that is not in the prepare of legal words, pass that discussion 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 office 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, peel, pee, per, pie, poe, pore, prep, pres,

      prey, 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: pismire, dint, hint, in, ina, inc, ind, ink, inn, ins, inti, into,

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

Note that the plan was written so that information technology volition non output the same misspelled discussion more than than in one case. (This is done by keeping a prepare of misspelled words that have been output.) If thecorrections() method returns an empty gear up, the program outputs the message "(no suggestions)". Since the corrections are stored in a tree prepare, they are automatically printed out in alphabetical club with no repeats.

The possible corrections that the program considers are as follows:

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

• Change whatsoever letter in the misspelled word to any other letter.

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

• Bandy any two neighboring characters in the misspelled word.

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

For constructing the possible corrections, you volition have to make extensive use of substrings. If w is a string, and sow.substring(0,i) is the cord consisting of the get-go i characters in w (non including the character in position i, which would be character numberi+1). Anddue west.substring(i) consists of the characters of due west from position i through the terminate of the string. For instance, ifch is a grapheme, then you can alter thei-thursday character of w to ch with the statement:

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

Besides, 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 past verified proficient

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 practice

rem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar torto

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

Unlock total access to Course Hero

Explore over 16 1000000 step-by-step answers from our library

Subscribe to 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 tortorconloneclair-conditioning, dictumrisus sumiametiur 50icetm 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 achiliad 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 air-conditioning, 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 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, consecteturair-conditioning,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dair-conditioning,dictum half-dozenmolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0s dues, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magac,ur laoreet. Nam risus ante, dapibus auair-conditioning, dictum vitae odio. Donec afinia pulvinar tortor nec facair conditioning,ipsum dentm ipsum dolor sit down ametonecs a molestie consequat, ultrair conditioning,ipsum dusceFusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortac,dictum half dozenur laoreet. Nam risus ante, 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 dues, 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 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 air-conditioning magna.usus dues, dapibus a molestie consequat, ultrices ac magna. Fusce duitris dues, dapibus a molestie consequat, ultrices ac magna. Fusce dui l0air conditioning, 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. 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 ante, dapibus a molestie conse0rem ipfdue south a molestieac,amet, consectetur adipiscifdue south a molestieair-conditioning,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 dues, 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 exercise

SelectFile.PNG Otpt.PNG

Student reviews

75% (28 ratings)

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

Post a Comment for "Once and Again Consignment Sabina Ohio"