Both Meters Are Idealized, the Battery Has No Appreciable Internal Resistance, and the Ammeter Reads

Lab 9: Sets in the Coffee Collection Framework For this week'southward lab,...

Lab 9: Sets in the Coffee Collection Framework

For this week'southward lab, yous will use 2 of the classes in the Coffee Collection Framework:HashSet andTreeSet. You will employ these classes to implement a spell checker.

Set Methods

this lab, you volition need to employ some of the methods that are defined in the Ready interface. Recall that if set is a Set, then the following methods are defined:

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

You will also need to be able to traverse a gear up, 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 ane word on each line. You will wait up words in this listing to check whether they are correctly spelled. To brand the list piece of cake to utilize, you can store the words in a prepare. Since there is no demand to accept the words stored in order, you can utilise aHashSet for maximum efficiency.

Use a Scanner to read the file. Y'all 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 candy, token by token, in a loop such as:

while (filein.hasNext()) {

     String tk = filein.next();

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

}

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

Commencement your main program by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this program,convert all words to lower example before putting them in the set. To make sure that you've read all the words, bank check the size of the set. (It should exist 72875.) You could besides employ the contains method to bank check for the presence of some common word in the set.

Checking the Words in a File

Once y'all have the list of words in a set, it's like shooting fish in a barrel to read the words from a file and cheque whether each give-and-take is in the set. Offset by letting the user select a file. You tin can either let the user type the name of the file or you can utilize 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(cypher);

           if (pick != JFileChooser.APPROVE_OPTION)

              return nada;

           else

              return fileDialog.getSelectedFile();

        }

Apply a Scanner to read the words from the selected file. In order to skip over any not-letter characters in the file, y'all tin can use the post-obit command merely after creating the scanner (where in is the variable proper name for the scanner):

in.useDelimiter("[^a-zA-Z]+");

(In this statement, "[^a-zA-Z]+" is a regular expression that matches whatsoever sequence of one or more non-letter of the alphabet characters. This essentially makes the scanner treat any non-letter of the alphabet the mode it would ordinarily care for a space.)

Y'all can then get through the file, read each word (converting information technology to lower case) and check whether the set contains the word. At this point, just print out any word that you detect that is not in the dictionary.

Providing a List of Possible Correct Spellings

A spell checker shouldn't just tell you what words are misspelled -- it should also give you lot a listing of possible correct spellings for that word. Write method

static TreeSet corrections(String badWord, HashSet dictionary)

that creates and returns aTreeSet<Cord> containing variations onbadWord that are contained in the dictionary. In your main plan, when y'all observe a word that is not in the prepare of legal words, pass that discussion 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. Hither, for example, is part of the output from a sample program when it was run with the HTML source of this page as input:

html: (no suggestions)

cpsc: (no suggestions)

hashset: hash set

treeset: tree set

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 next

wordlist: word list

getinputfilenamefromuser: (no suggestions)

jfilechooser: (no suggestions)

filedialog: file dialog

setdialogtitle: (no suggestions)

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

     it, lint, mint, nit, pint, tint

Note that the program was written so that information technology will not output the aforementioned misspelled discussion more once. (This is washed by keeping a fix of misspelled words that have been output.) If thecorrections() method returns an empty set, the program outputs the message "(no suggestions)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical order with no repeats.

The possible corrections that the plan considers are as follows:

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

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

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

• Swap whatever 2 neighboring characters in the misspelled word.

• Insert a space at any 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 utilize of substrings. If west is a cord, thendue west.substring(0,i) is the cord consisting of the first i characters in w (not including the character in position i, which would be character numberi+ane). Andwest.substring(i) consists of the characters of w from position i through the end of the string. For example, ifch is a character, then you tin can change thei-th grapheme of west to ch with the statement:

String due south = west.substring(0,i) + ch + w.substring(i+1);

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++) { ...

Answer & Explanation

Verified Solved by verified practiced

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 practise

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

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

Unlock full access to Course Hero

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

Subscribe to view respond

Step-by-footstep 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 licetm 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 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 fac1000 ipsat, ultrices ac magna. Fusce dui lectus, congue vel laoreet air conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetuuFusce dui lectus, congue vel laoreet air-conditioning, 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 amet, consecteturair-conditioning,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dac,dictum vimolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0s ante, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magazineac,ur laoreet. Nam risus ante, dapibus auair conditioning, dictum vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentm ipsum dolor sit down ametonecs a molestie consequat, ultrac,ipsum dusceFusce dui lectus, congue vel laoreet air conditioning, 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 nek ipsumiaor nec facilisis. Pellentesque dapibametm ipsumu1000 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 down amet, consectetur adipiscing elsuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magnicDiallen0entesque dapibus efficitur laoreet. Nam risus dues, dapibus a molestie conse0cing elit. Nam lacinia pulvinar tortor nec facilisfctum vitae odio. Donec aliquet. Lorem ipsum dolor sit down 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 dues, 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 air conditioning magna.uair conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur atriusce dui lectus, congue vel laoreet ac, dictum vitae odio. Doneac,ongueceair-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down 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 dues, dapibus a molestie conse0rem ipfdue south a molestieac,amet, consectetur adipiscifs a molestieac,fficitur laoreet. Nam risus dues, 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

Educatee reviews

75% (28 ratings)

montoyahaversidne.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 "Both Meters Are Idealized, the Battery Has No Appreciable Internal Resistance, and the Ammeter Reads"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel