CS 455 Programming Assignment 4Fall 2019 [Bono]Due: Wednesday, November 20, 11:59pmIntroduction and BackgroundIn this assignment you will get a chance to use some of the Collection classes and methods we havecovered recently. This will enable you to write a faster-running program with less effort than you wouldotherwise. In this assignment you will also get an opportunity to do your own design; the design outlinewe provided you is less constrained than in past assignments: you will be deciding on the exact interfaceand representation for your most of your classes. Youll also get some practice with command-linearguments and text file processing.This assignment concerns the game of Scrabble. You may know the game of Scrabble better as Wordswith Friends. If you want to try out Words with Friends yourself you can download the free app for yoursmartphone. However, the programming assignment is not to create the game itself, but to write aconsole-based program that finds all possible words that can be made from a rack of Scrabble tiles (so itcould help someone playing Scrabble). Well elaborate on the exact requirements of this assignment in thesection on the assignment below.A rack of Scrabble tiles (the little number is the score for playing that tile)The assignment filesNote: the blurbs below do not describe what each of these classes are, and how they fit together. For moredetails on that, see the section on the class design.The starter files we are providing for you on Vocareum are listed here. The files in bold below are onesyou create and/or modify and submit. The ones not in bold are ones that you will use, but not modify.More details about the java classes below are in the section on class design. The files are:WordFinder.java This class will contain the main method, and any other helper methods that youdesign. (You create this file.)AnagramDictionary.java All anagram sets from a dictionary. We have provided the interface foryou. This class is discussed more here.Rack.java Stores the current rack. You can decide on the representation and public methods for thisclass. We wrote the private static allSubsets method for you, discussed later.ScoreTable.java This class has information about how much each scrabble letter is worth. (Youcreate this file.)sowpods.txt The Scrabble dictionary we will be using. The version given here is all lower caseletters. Go here for an explanation of its odd name.testFiles Some data files and corresponding output for help in testing. The README.txt file in thatdirectory explains the files and how to use them.README See section on Submitting your program for what to put in it. Before you start theassignment please read the following statement which you will be signing in the README:I certify that the work submitted for this assignment does not violate USCs studentconduct code. In particular, the work is my own, not a collaboration, and does notinvolve code created by other people, with the exception of the resources explicitlymentioned in the CS 455 Course Syllabus. And I did not share my solution or parts of itwith other students in the course.Note: you may have additional files, see the section on the class design for more about this.The assignmentYou will be implementing a program that when given letters that could comprise a Scrabble rack, creates alist of all legal words that can be formed from the letters on that rack. To solve the problem you will alsoneed a scrabble dictionary (well provide that for you). Some particulars of the Scrabble dictionary: it onlyhas words of length two or more, and it includes all forms of a word as separate entries, e.g., singular plusplural, verb conjugations.For example, if your rack had the letters c m a l you could rearrange the letters to form the words calm orclam, but you could also form shorter words from a subset of the letters, e.g., lam or ma. Its generallydifficult to figure out all such sequences of the letters that form real words (unless you are a tournamentScrabble competitor who knows the Scrabble dictionary very well).For your program, you will display all such words, with the corresponding Scrabble score for each word,in decreasing order by score. For words with the same scrabble score, the words must appear inalphabetical order. Here are the results for a rack consisting of cmal (using the sowpods dictionary) inthe output format you will be using for your program (user input is shown in italics):Rack? cmalWe can make 11 words from cmalAll of the words with their scores (sorted by score):8: calm8: clam7: cam7: mac5: lac5: lam5: mal4: am4: ma2: al2: laWell provide you the Scrabble score for each letter later in this document .Heres more about exactly how to run your program and what happens:Your program will take an optional command-line argument for the dictionary file name. If that argumentis left off, it will use the Scrabble dictionary file sowpods.txt (see assignment files) from the samePA4 CS 455 2019/11/21 上午4(26http://bytes.usc.edu/cs455/curr/assgts/pa4/pa4.html#subsets 第 3 ⻚(共 8 ⻚)directory as you are running your program. If the dictionary file specified (either explicitly or the defaultone) does not exist, your program will print an informative error message (that includes the file name) andexit.The initial program output will be the message:Type . to quit.Then the program will run in a loop on the console, printing the prompt Rack? (as seen in the earlierexample) and reading and processing each rack you enter, until you tell it to exit. You tell the program toexit by typing in . at the prompt (i.e., a period). We arent use a command such as quit as the sentinel,since that could be a legal rack.We provided you a few sample data files, and corresponding correct reference output from running thoseon the sowpods.txt (the Scrabble dictionary given) in the testFiles directory. Your output must matchthe reference output character by character.The real game of Scrabble has only upper-case letters on tiles, but for our program well accept anysequence of non-whitespace characters as a legal rack. However, words will only be able to be formedfrom actual letters if thats whats in the given dictionary. E.g., if the rack given is abc@ you will reportthe words such as cab, but there will be no words containing @, since @ doesnt appear in anydictionary words.The program will work on both lower-and-upper case versions of dictionaries, but all processing will becase-sensitive. E.g., if the dictionary given has only upper-case versions of words, it will find words froma rack such as CMAL, but wont be able to find any words from the rack cmal.Some other differences between this program and Scrabble:The real game of Scrabble also has two blank wild-card tiles. Your program is not required tohandle blanks.In Scrabble you almost always have a rack of exactly seven letters. For this program you can enterany number of characters for a rack. If the rack has more than seven characters, you will reportwords from the dictionary that have more than seven characters too.This program just deals with forming words only from whats on the rack, it doesnt consider anytiles that are on the Scrabble board.This shows how to run your program: java WordFinder [dictionaryFile]Note: in this common format for showing Unix command-line syntax the square brackets (i.e., []) are notpart of the command that is typed: it is just syntax indicating that the command line argument shown isoptional.Additional program requirements are described in the following sections and summarized here:Approach. you are required to use the second approach discussed below, under Approach. Theclass design we started goes along with that approach.Efficiency. you will get more credit if you have an efficient solution. We discuss the efficiency ofthe approach you are required to use in the next two sections.Class design. you are required to design and implement the classes discussed in the section on classdesign. We will also be evaluating the quality of the fleshed out version of this design.PA4 CS 455 2019/11/21 上午4(26http://bytes.usc.edu/cs455/curr/assgts/pa4/pa4.html#subsets 第 4 ⻚(共 8 ⻚)Error checking. the only error you have to handle is if the dictionary file given is not found. Thiswas discussed in the section on the assignment. You are not required to check that all the wordsfrom the dictionary file are unique.README. as usual, you are required to submit a README file. See the end of this document forwhat needs to go in it for this assignment.Style / Documentation / Design. Also as usual, your program will be evaluated on style anddocumentation. See the section on grading criteria for more details.ApproachThere are two distinct ways to approach this problem. One is to read in the dictionary, and then for eachrack given, compare each word in the dictionary to that rack to figure out whether that word can beformed from some or all of the letters in that rack, creating a list of the legal words as you go. This isfaster to process the dictionary, but slower to process each rack.The second approach, which is the one you will be using for the assignment, involves preprocessing thedictionary so that you organize the words by the set of letters each one contains (this set is actually amultiset, because letters can appear more than once in a word; the rack itself is also a multiset). Then foreach rack youll generate all the subsets of that multiset of letters, and for each subset add all the wordsfrom the dictionary that have exactly the same elements as that subset. This is slower to process thedictionary, but once we do this processing, its faster to process each rack than the first approach. Thisapproach is explained in more detail in the following two sections.Its a little complicated to describe in big-O terms the time for each approach, but what makes the firstapproach slower for processing one rack is traversing the whole dictionary (which will typically be large)for each rack. For the second approach, the slow part of processing a rack is creating all the subsets. Theworst case for creating the subsets is if there are no repeated letters in the rack (i.e., largest number ofsubsets created). Even though generating the subsets for such a rack would take O(n * 2n) for a rack of nunique characters (because there are 2n subsets when there are no repeat characters, and n n steps to formeach subset), n will typically be small: for a 7-tile rack: 27 is only 128, times 7 is 896). In a solution wewrote using this approach, processing the sowpods dictionary took under half a second, and processing a7-character rack with no repeating characters, and consisting of the most commonly occurring letters inEnglish took under 15 milliseconds (this is test file testFiles/aestnlr.in). (Commonly occurringletters will result in a larger resulting word list.) These runs were done on Vocareum.Some of the time spent for processing a rack in the second approach is to get the list of anagrams for eachsubset; well discuss that further in the next section.The rest of the time spent processing a rack is to sort the resulting word list (which we would have to dofor either approach).You are required to use this second approach for the assignment; well go into further details about it inthe following sections.The AnagramDictionary classFor the approach were using, we said that you would organize the dictionary words by the (multi)set ofletters a word contains. If two words contain the same exact letters in a different order, they are calledanagrams of each other. If a rack (or subset of that rack) has all the same letters (and multiplicity of thoseletters) as a particular word in the dictionary, that word, plus all ofCS 455代做、Programming代写、java程序设 its anagrams from the dictionaryPA4 CS 455 2019/11/21 上午4(26http://bytes.usc.edu/cs455/curr/assgts/pa4/pa4.html#subsets 第 5 ⻚(共 8 ⻚)should all be added to the list of words reported by our WordFinder.You are required to create an AnagramDictionary class to handle this. It will have a getAnagramsOfmethod that finds all anagrams of a particular string efficiently. For, example, suppose we have a variable,dictionary, of type AnagramDictionary, that contains data from the sowpods dictionary. If we did thecalldictionary.getAnagramsOf(rlee)it would return an ArrayList of words with the contents: [leer, lere, reel] (not necessarily in thatorder). Note, rlee is not a real word, but the method does not require you to pass it a real word. But theanagrams returned are real English words.How to do this efficiently? One insight is that if we put two words into some kind of canonical form, thenwe could figure out if they are anagrams of each other by just comparing the canonical versions of themfor equality. This canonical form will be a sorted version of the characters in the word. In the earlierexample given the rack contained cmal. The sorted version of this rack is aclm. The first two wordslisted in the output are calm and clam, anagrams of aclm, or put another way, these first two wordsare the only dictionary words we can make using all the letters on the rack, and all the other words listedare anagrams of subsets of cmal.For full credit your AnagramDictionary is required to find all the anagrams of one String in time linear inthe size of the output set (not including the time to sort the letters in the String given).Finding all the subsets of the rackFinding all subsets of a multiset is a somewhat difficult recursion problem on its own, so to make thisassignment easier, we wrote the code for you to do that (static method allSubsets in Rack.java). Themethod is static because, like some other recursive methods we have written, it takes all of its data asexplicit parameters; also, this means allSubsets works regardless of what representation you choose foryour Rack objects (it will not be accessing any Rack instance variables). The solution is similar instructure to the method to compute all permutations of a string given in Section 13.4 of the textbook. Youwill likely have to write a wrapper method that calls allSubsets with the correct starting parameters.The allSubsets method uses a particular representation for the rack which well explain with an examplehere. Earlier we mentioned that a rack is a multiset of letters (set because we dont care about the order ofthe letters, and multiset because letters can appear more than once). Suppose our rack is:a b a d b bGathering together the like letters, we could rewrite this as aabbbd. We could also say that a appearswith multiplicity 2, b appears with multiplicity 3, and that d appears with multiplicity 1. allSubsetsexpects the rack information to be in two parallel arrays: one has the unique letters, and the other has themultiplicity of that letter at the same array index. The array of unique letters is actually a String, so we cando String operations on it. For the example given, we could create this rack representation as follows:// create variables for the rack aabbbdString unique = abd;int[] mult = {2, 3, 1};// example to show relation between values in unique and mult:for (int i = 0; i System.out.prinln(unique.charAt(i) + appears + multi[i] + times in the rack);PA4 CS 455 2019/11/21 上午4(26http://bytes.usc.edu/cs455/curr/assgts/pa4/pa4.html#subsets 第 6 ⻚(共 8 ⻚)}Like other examples of recursion over an array that weve seen, allSubsets will take a third argument, k,which is the starting position of the part of the array that this recursive call will process. So for this code,its the starting postion from which to find the subsets. So, for example, if we calledallSubsets(unique, mult, 1); // starts at position 1 in unique and multit would find all the subsets of the rack bbbd (i.e., it wouldnt consider the subsets that included any asin it).Class designUnlike the previous programs in this course, this time you are going to design your own classes, withsome guidance. Consequently, part of your style score will be based on the quality of your design.When doing an object-oriented design, you first come up with a candidate set of classes, choosing a namefor each, and identifying the responsibilities of each in the context of the larger program overall. We havedone that step for you here. We are requiring you to have at least the following four classes in yoursolution, with the responsibilities described. You are allowed to add more classes to your design as yousee fit. The four, with their overall responsibilities described, are:WordFinderThis contains the main method. This class will have a main thats responsible for processing thecommand-line argument, and handling any error processing. It will probably also have the maincommand loop. Most of the other functionality will be delegated to other object(s) created in mainand their methods.RackThis corresponds to the idea of the rack in the problem description. Thus, wherever your program isusing a rack, it should be using an object of type Rack. As previously discussed, we have alreadyprovided the code for a private static Rack method allSubsets.AnagramDictionaryThis will contain the dictionary data organized by anagrams. It is required to have at least the twopublic methods whose headers are given in the starter file. You are allowed to add other methods tothis interface. This class was discussed in more detail in the section about it.ScoreTableThis class has information about Scrabble scores for scrabble letters and words. In scrabble notevery letter has the same value. Letters that occur more often in the English language are worth less(e.g., e and s are each worth 1 point), and letters that occur less often are worth more (e.g., q andz are worth 10 points each). You may use hard-coded values in its data. Here are all the lettervalues:(1 point)-A, E, I, O, U, L, N, S, T, R(2 points)-D, G(3 points)-B, C, M, P(4 points)-F, H, V, W, Y(5 points)-K(8 points)- J, X(10 points)-Q, ZPA4 CS 455 2019/11/21 上午4(26http://bytes.usc.edu/cs455/curr/assgts/pa4/pa4.html#subsets 第 7 ⻚(共 8 ⻚)This class should work for both upper and lower case versions of the letters, e.g., a and A willhave the same score. Hint: You can index an array with a char that is a lower case letter by treatingit as an int and subtracting a from it (because the internal numeric codes for letters are allsequential). E.g., If your letter is d, (d - a) = 3 and if its e, (e - a) = 4.Although you havent done much class design yourself, you have seen many examples of well-designedclasses in the textbook, lecture, labs, and assignments in this class. We recommend you review thefollowing sections of the textbook that give hints on deciding what classes and methods would makesense for a program design, before you start on your own design: 8.1, 8.2, 12.1, and 12.2 (the last two ofthese were not in the original readings).One thing to keep in mind is you want the code that operates on some data to be in the same class thatcontains that data. One sign that your design doesnt have that feature is if your classes tend to have a lotof get and set methods and not much else. That would indicate that all the code operating on this data isoutside of the class itself.Hopefully weve made clear the importance of making all instance variables private. But even if you makeyour data private there are other ways to expose the implementation of your objects. For example, if youhave a class that contains an ArrayList, and also provide an accessor method for this ArrayList, it givesclients the ability to change the contents of that arraylist from outside of the object methods, possiblyinvalidating the object. (We discussed these types of issues and how to cope with them in the lecturediscussion of side effects in week 7.)You are welcome to add additional classes as part of your design. These ones would be designed andimplemented by you, of course. If you have more classes, just make sure the additional .java files are inyour Vocareum home directory when you submit the assignment. If a class is just used by one other class,you could put it in the same file as that class, or a separate file. If it is used by multiple classes, it shouldbe in its own file. Make sure you discuss these additional classes in your design write-up in yourREADME (including telling us where to find them).Development hintsAs usual, we recommend creating test drivers for any non-trivial class you implement to make it easier todebug your code. That should be pretty easy here, because the classes are somewhat independent fromeach other. (WordFinder is an exception since it already is a main program.)Youll want to test your complete program (and your AnagramDictionary) on a small dictionary file beforesubjecting it to sowpods.txt. We provided a sample small dictionary and input and corresponding outputfor some racks in the testFiles directory (more about that in the next paragraph). If you findAnagramDictionary-related bugs, you may want to use an even tinier dictionary for when you are singlestepping,etc.Once you have all your modules working, you can also check if your program produces the right answersfor sowpods.txt with the other test input files and corresponding output in the testFiles directory. Note:The testFiles/README.txt file describes whats what that directory.Grading criteriaThis program will be graded approximately 2/3 on correctness, 1/3 on design, style, and documentation.As usual we will be using the style guidelines published for the class. There was more about design issuesin the section on class design in this document. Another issue that will come into play is good use of thePA4 CS 455 2019/11/21 上午4(26http://bytes.usc.edu/cs455/curr/assgts/pa4/pa4.html#subsets 第 8 ⻚(共 8 ⻚)parts of the Java library we have learned about. E.g., its better to use one of the Java sort methods thanreimplementing it.README file / Submitting your programYour README file must document known bugs in your program, contain the signed certification shown nearthe top of this document, and contain any special instructions or information for the grader.In addition, for this assignment, your README must also document your design. This includes theapproach you took to solving the problem (i.e., description of the data structures and algorithmsinvolved). This was discussed in the section on approach. You will also include there information abouthow your class design relates to this approach, including what data structures and algorithms areencapsulated in which of your classes.When you are ready to submit the assignment press the big Submit button in your PA4 Vocareum workarea. Because you may have additional files in your program, it will try to compile all files in your workarea, and test the resulting program on the small dictionary data we gave you in testFiles (not onsowpods). As usual, you will want to submit for the first time well before the final deadline, so you havetime to fix any errors you get on the submit script.Passing these submit checks is not necessary or sufficient to submit your code (the graders will get a copyof what you submitted either way). (It would be necessary but not sufficient for getting full credit.)However, if your code does not pass all the tests we would expect that you would include someexplanation of that in your README. One situation where it might fail would be if you only completed asubset of the assignment (and your README would document what subset you completed.)转自:http://www.6daixie.com/contents/9/4450.html