Exercises


  1. Create a String literal using the first two sentences of this exercise. You will create a WordCount class to parse through the text and count the number of instances of each word. Punctuation should not appear in the word list, either as part of a word or as a separate "word." Use a map to store the frequencies. The WordCount class should be able to return a set of strings, each showing the word and its frequency. Track the frequency without respect to case. In other words, two words spelled the same but with different case are the same word. Hint: You can use the regular expression \W+ in conjunction with the String split method in order to extract the words from the String.

  2. Create a class, Name, that declares a single string field. Create an equals method on the class that uses the string for comparisons. Do not create a hashCode method. Build a test that verifies the contract of equality for Name.

  3. Create a Set<Name> object that contains a variety of Name objects, including new Name("Foo"). Verify that the Set contains method returns false if you ask the Set whether or not it contains the Name("Foo") instance. Show that if you create:

     Name foo = new Name("Foo"); 

    the set does contain foo.

  4. Modify the test for Name (see the previous question) to pass only if the list does contain new Name("Foo").



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net