Starting from:

$25

CS1027 - Computer Science Fundamentals II - Assignment 2 - Solved

Introduction  
Squabble is an interesting word game in existence, and it has a few special features that make this game the preferred one in the race. The rules are easy, it gives players six chances to guess a randomly selected five-letter word. You can enter a total of six words, meaning you can enter five burner words from which you can learn hints about the letters and their placements. Each letter in your guess is labeled with respect to a mystery word as a correct letter, a used letter, or an unused letter. For instance, if the mystery word was “ADULT” but your guess was “AGENT”, then the labelling would be as follows: A correct, G unused, E unused, N unused, and T correct. That information can inform the next guess. Each subsequent guess helps narrow down what the mystery word could be. Although much thought was put into these game rules, our tools will to be less constrained to broaden the possibilities. For example, both length of word and number of guesses will be arbitrary. Along with lifting these numerical constraints, we are going to lift textual constraints in our tools to allow different letters other than A-Z. In particular, the tools will be able to deal with characters other than A-Z allowing for more variants for a Squabble-esque game. We retain the idea of labelling a words’ letters according to a mystery word.  

The main class, WordLL, will be a non-graphical text-based utility. Simple interfaces for WordLL are provided in WordLLExamples. Below is an example run of WordLLExamples in action where bold words are guesses entered by keyboard and lines starting with “Word:” show a history of results.  Note that letters are decorated/surrounded with either “-“, ”+”, or “!” which correspond to unused, used, and correct. In the original Squabble game, colours indicate this information: unused letters are grey(-), used are yellow(+), and correct are green(!).   

enter a word (XX to stop):BOOK  Word: -B- -O- -O- -K-   enter a word (XX tostop):LONDON  

Word: +L+ -O- -N- +D+ -O- -N-  Word: -B- -O- -O- -K-   enter a word (XX tostop):SHELF Word: -S- -H- -E- !L! -F-  Word: +L+ -O- -N- +D+ -O- -N-  Word: -B- -O- -O- -K-   enter a word (XX tostop):ADULT  You got it!  

Assignment 2  
 

      

  

Word: !A! !D! !U! !L! !T!  

Word: -S- -H- -E- !L! -F-  Word: +L+ -O- -N- +D+ -O- -N-  Word: -B- -O- -O- -K-   enter a word (XX to stop):XX  

Provided files   
The following is a list of files provided to you for this assignment. Do no not alter  

LinearNode.java. The other two files are provided to help you understand the requirements of the assignment and are not to be submitted.  

•       LinearNode.java  (from the notes)  

•       TestWordLL.java (some tests to check your code which may differ from Gradescope’s tests)  

•       WordLLExamples.java (a collection of uses for your finished code)  

•       words (a text file with many words)  

Classes to Implement  
For this assignment, you must implement four Java classes: Letter, Word, WordLL, and ExtendedLetter. Follow the guidelines for each class below.  

In all these classes, you can implement more private (helper) methods, if you want to, but you may not implement more public methods. You may not add instance variables other than the ones specified below nor change the variable types or accessibility (i.e. making a variable public when it should be private). Penalties will be applied if you implement additional instance variables or change the variable types or modifiers from what is described here. Letter.java  

This class represents a single letter that will be used in the game. Each game letter also has an accompanying integer label which indicates whether it is used, unused, or correct with respect to the mystery word.  

The class must have the following private variables, and constants:  

•        letter (char)   

•        label (int)   

Assignment 2  
 

      

  

•        UNSET, UNUSED, USED, CORRECT (int) (constants that have unique values; these are the possible values for the “label” instance variable)  The class must have the following public methods:  

•        public Letter(char c) [constructor]  

•        Initialize label to UNSET and set the value of instance variable letter to c  

•        public boolean equals(Object otherObject)  

•        First checks whether otherObject is of the class Letter, and if not the value false is returned. If otherObject is of the class Letter, then the “letter” attributes of otherObject and this object are compared: If they are the same the value true is returned, otherwise false is returned.   

•        public String decorator()  

•        Returns “+” (if the “label” attribute is USED), “-“ (if the “label” attribute is UNUSED), “!(if the “label” attribute is CORRECT), or “ “  (if the “label” attribute is UNSET; note this is a space).         

•        public String toSt”ring()  

•        an overridden method that gives a representation of letter & label which uses the helper method decorator. The String returned is of the form “dCd”, where C is the “letter” attribute of this object and d is the String returned by the decorator() method.   

•        from the Introduction, we can see some examples of Letter.toString():  ª “+A+”, 

“+C+”, “-C-”, “!E!”, “-P-”, “-T-”  

ª  A, C are letters that are USED

ª  P, T are letters that are UNUSED  

ª  E in the fourth location is CORRECT  

•        public void setUnused()  

•        used to change the value of attribute “label” to UNUSED  

•        public void setUsed()  

•        used to change the value of attribute “label” to USED            

•        public void setCorrect()  

•        used to change the value of attribute “label” to CORRECT   

•        public boolean isUnused()  

•        returns true if the attribute “label” is set to UNUSED  

•        otherwise returns false  

•        public static Letter[] fromString(String s)  

•        Produces an array of objects of the class Letter from the string s given as parameter.

For each character in s a Letter object is created and stored in the array. The Letter

Assignment 2  
 

      

  

objects are stored in the array in the same order in which the corresponding characters appear in s.  

Word.java  
This class represents a word in the game that is comprised of any number of letters. Each letter is represented by a Letter object. The Letter objects are stored in a linked list formed by objects of the class LinearNode. Each node in the linked list stores an object of the class Letter. The most important instance method of this class is labelWord which labels Letter objects with respect to a mystery word. This is the trickiest method of this assignment.   The class must have the following private variables:  

•       firstLetter (LinearNode<Letter>): A reference to the first node in the linked list representing the word corresponding to this object.  

The class must have the following public methods:  

•       public Word(Letter[ ] letters) [constructor]  

•       Initialize the Word object so the Letter objects in array “letters” is stored within its linked structure. Instance variable firstLetter must point to the first node of the linked list.  

For example, the invocation to the constructor passing as parameter an array of Letter objects corresponding to guess “BOOK” in page 1 would create the following linked list:  

 

•       public String toString()  

•       Creates a String of the form: “Word: L1 L2 L3 … Lk”, where each Li is the string produced by invoking the toSting method on each Letter object of this Word.  

 

Assignment 2  
 

      

  

•       from the Introduction, we can see examples of the output of this toString() method:  

ª  “Word: +A+ -C- -C- -E- -P- -T-”  

ª  “Word: -B- -O- -O- -K-”

  

•       public boolean labelWord(Word mystery)  

•       takes a mystery word as a parameter and updates each of Letters’ “label” attribute contained in this Word object with respect to the mystery word  

•       returns true if this word is identical in content to the mystery word  

•       To understand how the “label” attribute of the Letter objects stored in the linked list of a Word object are updated, consider an example. Suppose that the mystery word is “APPLE” and that this object stores Letter objects corresponding to the word

“BOOK”, then the “label” attributes of the Letter objects would be updated as follows:  

ª  label for Letter object corresponding to ‘B’ is UNUSED  

ª  label for Letter object corresponding to ‘O’ is UNUSED  

ª  label for Letter object corresponding to ‘O’ is UNUSED  ª label for Letter object corresponding to ‘K’ is UNUSED  

WordLL.java  
This class is a central repository for information about a WordLL game: It stores a mystery word and all word guesses tried so far. It keeps a history of the past word guesses in a linked structure. Its name is a bit of play on words—Word-Linked-List.  

The class must have the following private variables:  

•       mysteryWord (Word)  

•       history (LinearNode<Word>)  

  

Assignment 2  
 

      

  

 

The class must have the following public methods:  

•       public WordLL(Word mystery) [constructor]  

•       Initialize an empty history  

•       set the mysteryWord attribute to the parameter mystery  

•       public boolean tryWord(Word guess)  

•       takes a Word as an argument to test against this games’ mystery word   

•       updates the label of all the letters contained within Word guess (using labelWord) and adds Word guess to the front the of history (you must create a node of the class LinearNode, store the Word guess object in it and then link this node to the front of the linked list pointed by history)  

•       returns true if the word represented by guess is identical to the word represented by mysteryWord, otherwise returns false  

•       public String toString()  

•       Creates a String representation of the past guesses with the most recent guess first. From the Introduction, we can see examples of the strings produced by this method toString() after every guess. For instance after the third guess in the example of page 1 this is what results of invoking method toString():  

                                                                                     

 ª        “Word: -S- -H- -E- !L! -F-        Word: +L+ -O- -N- +D+ -O- -N-  

      Word: -B- -O- -O- -K-”  

Assignment 2  
 

      

  

Note the end of line “\n” after each word.

  

ExtendedLetter.java  
This class is a subclass of Letter and extends the functionality. Instead of relying on a single char to represent the content of a Letter object, objects of this class will use a String instance variable and will further introduce the concept of being related to other ExtendedLetter objects. This class adds more features to broaden the notion of a letter that will be used in the game.   

The class must have the following private variables, and constants:  

•       content (String)   

•       family (int)  

•       related (boolean)  

•       SINGLETON (int) constant equal to -1  The class must have the following public methods:  

•       public ExtendedLetter(String s) [constructor]  •        Initialize instance variables of the superclass   

ª  super(c) where c is an arbitrary char (it doesn’t matter which since it will not be used)  

•       Initialize the instance variables as follows:  

ª  content is set to the String parameter s  

ª  related is set to false  

ª  family is set to SINGLETON  

•       public ExtendedLetter(String s, int fam) [constructor]  

•       Initialize instance variables of the superclass   

ª  super(c) where c is an arbitrary char (it doesn’t matter which since it will not be used)  

•       Initialize the instance variables as follows:  

ª  content is set to the String parameter s  

ª  related is set to false  

ª  family is set to the int parameter fam; this is a positive number which indicates that any ExtendedLetter object with the same value in instance variable family will be consider related to this ExtendedLetter object  

•       public boolean equals(Object other)  

•       return false if the parameter other is not an instanceOf ExtendedLetter   •  otherwise   

Assignment 2  
 

      

  

ª  it will set the instance variable related of this object to true if the family instance variable of other is the same as this.family.  

ª  return true if the instance variable content of other is equal to the instance variable content of this object;   

ª  otherwise it returns false  

              

•       public String toString()  

•       an overridden method that gives a String representation of this ExtendedLetter object   

•       If this ExtendedLetter object is unused (its label instance variable has value  

UNUSED) and its instance variable related has value true, return the string “.A.” where A is equal to this.content. For example, if this.content is “%” and this.related is true, this method would return “.%.”.  

•       Otherwise, this method should return a string “+A+”, “!A!”,“-A-”, or “ A ” depending of the value returned by method decorator() from the superclass, where A is equal to this.content.  

  

  

•       public static Letter[] fromStrings(String[] content,int[] codes)  

•       Creates an array letters of Letter objects of the same size as the size of the array content received as parameter. This array letters will be returned by the method after storing in it the following information:  

•       If parameter codes is null then the i-th entry of array letters will store an ExtendedLetter object created with the constructor ExtendedLetter(content[i]).  

•       If codes is not null, then the i-th entry of array letters will store an ExtendedLetter object created with the constructor ExtendedLetter(content[i],codes[i]).  

To Run the Program  
If you are running the program from the terminal, place all files in the same directory, compile them with javac and run the program by typing java WordLLExamples.  

If you are using Eclipse, put the file called words in the root folder of your project (not inside the src folder) and run the WordLLExamples class.  

More products