112 Modify the Anagrams program to enforce the constraint th

11.2) Modify the Anagrams program to enforce the constraint that words must be at least four letters long. (This is not necessary if all shorter words are removed from the file words.txt.)

11.4) In the constructor for the Anagrams class (Figure 11-7, line 14), the add() method is invoked many times on the Set words. If words is an OrderedList, under what circumstances will this take the largest possible amount of time (for a given number of words n)? Is this likely to occur?

11.8) When an element is added to a binary search tree, a new node is added as a child of an existing node. Before the addition, was this existing node always, sometimes, or never a leaf? Explain.

11.9) In the worst case, searching a binary search tree takes linear time. Can this happen when performing a binary search (Section 8.2) on a sorted array? Explain.

11.12) Analyze the worst-case running times of the methods in the LetterCollection class. Give your results in terms of n, the size of the set, and (where appropriate) w, the length of the word involved.

11.17) Which approach would have better cache performance: chaining or open addressing? Explain.

Solution

regulate the Anagrams program to enforce the constraint that words need to be at the least four letters lengthy…. display more adjust the Anagrams application to enforce the constraint that phrases should be at least 4 letters long. (This isn\'t always important if all shorter phrases are removed from the record words.txt.) import java.util.Scanner; /** the game of Anagrams. */ public class Anagrams /** For studying from the console. */ public static very last Scanner enter = new Scanner(system.in); /** Letters in the bag. */ private LetterCollection bag; /** Letters inside the pool. */ non-public LetterCollection pool; /** big set of criminal words. */ private Set phrases; /** phrases scored via player 1. */ personal Set words1; /** phrases scored by using player 2. */ private Set words2; /** * read within the dictionary from the record “anagram-words” and create * the letters. */ public Anagrams() bag = new LetterCollection(genuine); pool = new LetterCollection(fake); words = new OrderedList(); strive Scanner input = new Scanner(new java.io.record(“words.txt”)); whilst (enter.hasNextLine()) phrases.add(enter.nextLine()); catch (java.io.IOException e) e.printStackTrace(); system.go out(1); words1 = new OrderedList(); words2 = new OrderedList(); /** Play till someone gets 5 words. */ public void play() whilst (authentic) machine.out.println(“participant 1 TO PLAY”); playTurn(words1, words2); if (words1.size() == five) device.out.println(“participant 1 wins!”); return; system.out.println(“participant 2 TO PLAY”); playTurn(words2, words1); if (words2.size() == 5) device.out.println(“participant 2 wins!”); return; /** Play one turn for the specified player. */ public void playTurn(Set player, Set opponent) pool.add(bag.draw()); machine.out.println(“Letters in pool:n” + pool); device.out.println(“participant 1’s words: ” + words1); device.out.println(“player 2’s phrases: ” + words2); device.out.print(“Your play: “); String play = enter.nextLine(); int spaceIndex = play.indexOf(‘ ‘); if (spaceIndex != -1) // Stolen word String phrase = play.substring(zero, spaceIndex); if (!(phrases.includes(word))) device.out.println(“Sorry, ” + phrase + ” is not a phrase.”); else String stolen = play.substring(spaceIndex + 1, play.length()); participant.add(word); opponent.dispose of(stolen); pool.upload(stolen); pool.take away(phrase); else if (!(play.equals(“”))) // regular play if (!(phrases.contains(play))) device.out.println(“Sorry, ” + play + ” isn\'t a word.”); else participant.add(play); pool.take away(play); system.out.println(); /** Create and play the game. */ public static void most important(String[] args) Anagrams recreation = new Anagrams(); machine.out.println(“Welcome to Anagrams.”); device.out.println(); device.out.println(“To make a phrase from the pool, enter it.”); system.out.println(“To steal a word, input the brand new phrase, a” + ” space, and the word being stolen.”); system.out.println(“To pass, just hit return.”); gadget.out.println(); game.play(); • display much less

11.2) Modify the Anagrams program to enforce the constraint that words must be at least four letters long. (This is not necessary if all shorter words are remov

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site