tags:

views:

105

answers:

3

Hello,

Well all the questions I have been asking regarding TreeSet, I have wasted your valuable time. I am really sorry because I believe I mis-understood the question. It doesn't help when you got no one to talk to about this assignment. The real question was related to anagram assignment. In the first question it ask "To the Anagram class add a new public instance method called LetterSet() that takes a string as an argument and returns the characters in the argument as a set. I believe I have done right. But tell me if I am wrong? Then it asks to create empty instance in the constructor which I have done. So far it has not asked to add any letters to the Table. The reason I have used statics in my previous questions because all the examples I have given were based on public static void methodname. Feel free wipe of all the points I have gained, I don't deserve it. Here is what I have done...

public class Anagram  
{
   private static  Set<String> Table = new TreeSet<String>();
   public Anagram()
   {
    super();
    this.Table = new TreeSet<String>();
   }    

  public Set<String> LetterSet(String phrase)
  {
     return anagramTable;
  }
}
A: 

I would say you should be returning a set of chars rather than strings. Basically the method, which is perfectly reasonable as static, should take a string and push each letter into a set then return that set.

stimms
Yes it complies. It's one of those things I don't get. Can't wait for the answers though. Thanks for your valuable advise.
+1  A: 

The method signature should be like this:

public static Set<Character> LetterSet(String phrase)

because you're asked to return a set of characters.

The rest of the method is pretty straightforward: create a set, that you want to return as a result. If you need a sorted output, TreeSet is the preferred implementation.

Then get the chars from the phrase and put the chars one by one on the set. Finally, return it.

Andreas_D
And the method name should start with lowercase.
Steve Kuo
@Steve - of course, but obviously the assignment asks for this unconventional method name ;)
Andreas_D
@Steve,Andreas obviously this is a life lesson in the fact that customers always have poor requirements.
glowcoder
A: 

As others have said, while a set of String will work, a set of Character makes more sense because that you'll only ever be storing single characters.

Don't make Table static. Here's why:

The constructor, Anagram(), returns an object of type Anagram. Objects of type Anagram contain a private field called Table, which, because it starts with static, belongs to the class. (In other words, there is only one Table, and all Anagram objects share it). Initializing it as this.Table doesn't help: this.Table is a reference. Each object has it's own reference, sure, but they refer to the same TreeSet!

For example, you create an Anagram object ag1 with Anagram ag1 = new Anagram(). This calls the constructor that you made. You use it to process a word, so now Table has some letters in it. Now you want to do another word, so you make a new object. Anagram ag2 = new Anagram(). Your Table isn't empty, it still has the letters from the last word!

The solution is to just drop the static keyword before Table :)

Next: are you sure you really want to use a Set? Sets don't allow duplicates. For instance, "aaazzz" and "az" will produce the same Set, but they aren't anagrams.

John C