views:

100

answers:

2

I have instantiated an object like this:

GraphMatrixDirected<String, Integer> g

g is passed to a function like this:

floyd(g);

floyd's signature looks like this:

public void floyd(GraphMatrixDirected<V,E> g) 

Eclipse gives me an error saying:

The method floyd(GraphMatrixDirected<V,E>) in the type GraphMatrix<V,E> is not applicable for the arguments (GraphMatrixDirected<String,Integer>)

What do I need to do to fix it?

Edit1:

abstract public class GraphMatrix<V,E> extends AbstractStructure<V> implements Graph<V,E>

Edit2:

public interface Graph<V,E> extends Structure<V>

public abstract class AbstractStructure<E> implements Structure<E>

public interface Structure<E> extends Iterable<E>
/* JDT added extension of Iterable for Java 5 */

Edit 3: Note: Funtion was changed from floyd to AllPairsShortestPath.

public void AllPairsShortestPath(GraphMatrixDirected<V,E> g) 
// post: g contains edge (a,b) if there is a path from a to b 
{
    Iterator<V> witer = g.iterator();
    while (witer.hasNext()) 
    {
        Iterator<V> uiter = g.iterator();
        V w = witer.next(); 

        while (uiter.hasNext())
        {
            Iterator<V> viter = g.iterator(); 
            V u = uiter.next(); 

            while (viter.hasNext()) 
            {
                V v = viter.next(); 

                if (g.containsEdge(u,w) && g.containsEdge(w,v)) 
                {
                    Edge<V,E> leg1 = g.getEdge(u,w);
                    Edge<V,E> leg2 = g.getEdge(w,v); 

                    Integer leg1Dist = (Integer)leg1.label(); 
                    Integer leg2Dist = (Integer)leg2.label(); 
                    Integer newDist = (Integer)leg1Dist+leg2Dist;

                    E newDistE = (E)newDist;

                    if (g.containsEdge(u,v)) 
                    {
                        Edge<V,E> across = g.getEdge(u,v);
                        Integer acrossDist = (Integer)across.label(); 

                        if (newDist < acrossDist)
                        {
                            across.setLabel(newDistE);
                        } 
                    } 
                    else 
                    {
                        g.addEdge(u,v,newDistE);
                    }
                }
            }
        }
    }
}
+3  A: 

Try

public <V, E> void floyd(GraphMatrixDirected<V,E> g) 

(This should probably be static too).

Otherwise, we would have to see how you create the object containing this method, and in particular what types you assign to V and E (if V was String and E was Integer, it would work, but apparently it isn't).

I understand you have something like

class GraphMatrixDirected<V,E> extends GraphMatrix<V,E> { ... }

Then you could keep the method signature as you had it (actually if it is defined in GraphMatrixDirected and it is an instance method, you probably don't want another GraphMatrixDirected parameter to it, but just use this), and just use:

GraphMatrixDirected<String, Integer> g = new GraphMatrixDirected<String, Integer>();

Note that I don't endorse this design, but this is beyond the point.

Dimitris Andreou
This is what I currently have now, and it is erroneous.
Nathan
+3  A: 

Perhaps you meant floyd to be a generic method?

public <V,E> void floyd(GraphMatrixDirected<V,E> g)

Otherwise, whatever generic type floyd is a member of need to have <V,E> parameterized as <String,Integer>.

If it's true that floyd belongs to a generic type, and yet floyd also needs to be a generic method with its own type parameters, you may want to choose different names, so as not to hide one another.

References


On generic type vs generic methods

The choice between which solution route to take depends on what floyd does and other things. One essential question is this: do you consider floyd to be a method that belongs specifically to the generic type GraphMatrixDirected<V,E> (answer is probably no) or is it rather a generic utility method that works with any Graph<V,E>? (answer is probably yes).

For example and guidance, we can also take a look at how Java Collections Framework is structured:

  • interface List<E> - a generic type that defines basic functionality for the type
    • specifies boolean add(E e), E get(int index), etc
  • class Collections - provides static utility generic methods
    • static void shuffle(List<?> list)
    • static <T extends Comparable<? super T>> void sort(List<T> list)
    • static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

Assuming that floyd is indeed an implementation of Floyd-Warshall all-pairs shortest path algorithm, I'd argue that it should be a static utility method to say a Graphs class, and works with any Graph<V,E>.

See also, Effective Java 2nd Edition

Generic types vs methods:

  • Item 26, Favor generic types
  • Item 27, Favor generic methods

Interfaces:

  • Item 18, Prefer interfaces to abstract classes
  • Item 19, Use interfaces only to define types
  • Item 52, Refer to objects by their interfaces
polygenelubricants
The link to "Angelika Langer's Java Generic FAQs" has a typo. The correct URL is:http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
Jim Dodd
@Jim: thanks for correction; noted. Will revise on my own along with more update once I have enough information about the question.
polygenelubricants
@polygenelubricants: Do you see anything new now?
Nathan
@00PS: I didn't see anything new from YOU. Did you read the FAQs I linked to especially with regards to generic types vs generic methods? Anyway, I'll try to provide more info but unless you give more info and/or do your own reading, I can only guess at what's happening.
polygenelubricants
@polygenelubricants: What else would you like to see? I'm a little unsure about how I can fix my code.
Nathan
@00PS: if you can post `floyd` method in its entirety, we can comment whether or not it should be a generic `static` utility method. I'm 90% confident that it is.
polygenelubricants
@polygenelubricants: Here it is.
Nathan
@00PS: try `public static <V,E> void AllPairsShortestPath(GraphMatrixDirected<V,E> g)` for a quick-fix attempt for now; however, I will comment on the code in more detail tomorrow. There's a lot to cover, e.g. for-each loop, preferring primitives to boxed primitives, etc.
polygenelubricants