tags:

views:

97

answers:

4

Hello,

In the instructions I have been asked to "declare a interface type to hold a map with sets of characters as its keys, and with sorted sets of strings as values". All this time I have been using TreeSets. Now I am not sure, I am now thinking of using TreeMap. Code below is demo TreeMap I have used. Firstly is it acceptable to use TreeSet instead of TreeMap as per instruction above. Secondly I am getting an error "non-static variable names cannot be referenced from a static context", when using TreeMap for methodB()? Thanks.

public class MyMates  
{

  private TreeMap names = new TreeMap();
  private static String[] name1 = null;
  private static String[] name2 = null;
  private static String[] name3 = null; 


  public MyMates()
  {
    super();    
    names = new TreeMap();  
  }

  public static void methodASet()
  {

    String[] name1 = new String[] {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
    String[] name2 = new String[] { "Alan", "Amy", "Jeremy", "Helen", "Alexi"};
    String[] name3 = new String[] { "Adel", "Aaron", "Amy", "James", "Alice" };
  }

  public static String methodB(String aTemp)
  {

    for (int i = 0; i < name1.length; i++) 
    {
     names.add(name1[i]);
    }
    System.out.println(names);

    for (int i = 0; i < name2.length; i++)
    {
       names.add(name2[i]);
    }   
    System.out.println(names);

    for (int i = 0; i < name3.length; i++)
    {
       names.add(name3[i]);
    }   
    System.out.println(names);


   return aTemp
  }


   public static void populateTable()
  {
    girlFriends myList = new girlFriends();

    names.addAll(myList.getNames()); // same error here
  }
+1  A: 

The "non-static variable names cannot be referenced from a static context" is easy to resolve.

You can remove the "static" modifier of your methodB().

Another solution is to declare your class member "names" as static.

Your method methodASet() has a bug !!! You redeclare name1, name2, and name3 as local variables. So the static class members are never set.

A TreeSet and a TreeMap haven't the same function :

  • A TreeMap is a Map (inteface java.util.Map), that is to say an association key/value (where keys are ordered)
  • A TreeSet is a Set (interface java.util.Set) : an ordered collection of objects where all objects are unique in the set, without the "key" notion of the Map.

So, you have to choose the representation (Map or Set) in relation with your needs (access to the objects by a key or not).

Benoit Courtine
thanks Benoit Courtine
+4  A: 

As R. Bemrose said in a comment, you're not being asked for an implementation, just an interface. So don't worry about the implementation. A map lets you put a value (in your case, a sorted set of Strings) in for a particular key (in your case, a set of characters), and use the same key to retrieve the same value. What would that interface look like? That's the question.

Carl Manaster
@dowln That's actually the best advice so far. Try Your luck at defining an interface and ask the community if You have problems with it.
Dave
+1  A: 

It looks like private TreeMap names isn't static so it can't be accessed from a static method. Perhaps you need to brush up on your static variables/methods? http://leepoint.net/notes-java/flow/methods/50static-methods.html

You have to remember that methodASet and methodB are different methods and variables in methodASet cannot be seen by methodB. When you declare String[] name1 you're making a new variable of type String[], not using the class variable name1 (the one you set to null). Remove the "String[]" from the variables in methodASet to reuse the class variables.

Thinking about it, does TreeMap even have add() methods? I think you might want to switch to the aforementioned TreeSet or change your add() methods to put().

For populateTable(), are you sure that girlFriends is a type? Where is it coming from? Are you sure you don't mean for myList to be of a standard java collections type?

Hope my (mostly convoluted) post helps!

Edit: Blast! I think you're right. It looks like he's only got to write the interface, not the implementation.

Edit: Static examples

Static method:
Class.doSomething()

Static variable:
Class.count_something

Class method:
ObjectType obj = new ObjectType()
obj.doSomething()

Class variable:
ObjectType obj = new ObjectType()
obj.count_something
shookster
girlFriends is coming fron another class that has string set return type. I need to figure out if static is to be used?
Static methods and variables are used when you want the uninstantiated class to have methods and variables (without being instantiated). Most of the time you want the instantiation (ObjectType obj = new ObjectType()) to have it's own variables irrespective of what other instantiations are doing.Edited my above post to help explain
shookster
A: 

This is kind of an aside since it looks like your instructions actually want you to create an interface, not an implementation. However, given the task of implementing this interface, if consistently using Java terminology (map, set, character and string), the instructions seem to want you to maintain this data structure:

Map<Set<Character>, SortedSet<String>> map;

If that's the case, a TreeMap is not a good choice of implementation since it is very difficult to enforce an ordering on non-sorted Sets. Set doesn't implement Comparable for starters and creating a Comparator for your TreeMap would require some complex logic.

If the above is what your instructor is expecting, I would suggest using a HashMap which has no problem using a Set as a key type:

map = new HashMap<Set<Character>, SortedSet<String>>();
Mark Peters