1

I have created 3 classes Edge Vertex and Graph. The graph is the main class. The following is the code for the 3 classes:

Edge

public class Edge {
Vertex vertex1;
Vertex vertex2;
int weight;
String name;
int edgeNumber;

Edge(){
    weight=-1;
    name=null;
    edgeNumber=-1;
}
Edge(Vertex vertex1,int edgeNumber,Vertex vertex2,int weight,String name){
    this.vertex1=vertex1;
    this.vertex2=vertex2;
    this.weight=weight;
    this.name=name;
    this.edgeNumber=edgeNumber;
}

public void printEdge(Edge e){
    System.out.println("Edge Number :"+edgeNumber);
    System.out.println(e.vertex1);
    System.out.println(e.vertex2);
    System.out.println(e.name);
    System.out.println(e.weight);
}

}

Vertex

public class Vertex {
int vertexId;
String name;

Vertex(){
    vertexId=-1;
    name=null;
}
public void createVertex(Vertex ver1,int id,String vName){
    ver1.vertexId=id;
    ver1.name=vName;
}

}

Graph.java

public class Graph {
int numVertices;
int numEdges;
int[][] matrix=new int[20][20];
Vertex[] vertexList =new Vertex[10];
Edge[] edgeList=new Edge[10];
/*
 * @param args the command line arguments
 */
// Intitalise the graph
//All values in matrix are zero
//Vertex names are also taken from name
Graph(int[][] vertexArr,int numVertices){
    this.numVertices=numVertices;

    for(int i=0;i<numVertices;i++){
        for(int j=0;j<numVertices;j++){
            vertexArr[i][j]=0;
        }
    }
}
public int graphDegree(Graph G,int vertex){
    int i,degree=0;
    for(i=0;i<numVertices;i++){
        if(matrix[vertex][i]==1)
            degree++;
    }
    return degree;
}
public void addEdge(Edge e1,int edgeIndex,Vertex v1,Vertex v2,String nameIn,int weightIn){
    int old=matrix[v1.vertexId][v2.vertexId];
    if(old!=0)
        System.out.println("The edge already exists");
    //creating new edge
    matrix[v1.vertexId][v2.vertexId]=1;
    matrix[v2.vertexId][v1.vertexId]=1;
    e1.vertex1=v1;
    e1.vertex2=v2;
    e1.name=nameIn;
    e1.weight=weightIn;
    edgeList[edgeIndex]=e1;
    numEdges++; 
}
public void printGraph(Graph G){
    System.out.println("Number of vertices in the graph : "+G.numVertices);
    for(int i=0;i<numEdges;i++){
        G.printEdge(edgeList[i]);
    }

} 
public static void main(String[] args) {

    int vertexCount=5;

    Vertex v1=new Vertex();
    Vertex v2=new Vertex();
    Vertex v3=new Vertex();
    Vertex v4=new Vertex();
    v1.createVertex(v1,0,"Delhi");
    v2.createVertex(v2,1,"Noida");
    v3.createVertex(v3,2,"Mumbai");
    v4.createVertex(v4,3,"Kanpur");
    int[][] graphStore=new int[vertexCount][vertexCount];
    Graph g1=new Graph(graphStore,vertexCount);

    Edge edge1=new Edge();
    Edge edge2=new Edge();
    g1.addEdge(edge1,0,v1,v2,"DtoN",10);
    g1.addEdge(edge2,1,v2,v3,"NtoM",50);
    g1.printGraph(g1);
}

}

The printGraph() function shows the error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: graph.Graph.printEdge
at graph.Graph.printGraph(Graph.java:63)
at graph.Graph.main(Graph.java:86)

I am not able to make the print graph function. How do i print all the edges in the graph?

3
  • 2
    printEdge() is a method defined in class Edge. G is an object of class Graph. How can you access printEdge() method with G? Commented Sep 28, 2015 at 14:32
  • i strongly suggest to read how static methods and constructors work. you seem to misunderstand both concepts. Commented Sep 28, 2015 at 14:42
  • @sifho Thanks for the input, I called the printgraph() function using edgelist and now this function is working. Commented Sep 28, 2015 at 14:55

2 Answers 2

1
  1. Change printEdge() method to static

    public static void printEdge(Edge e)
    
  2. Your printGraph() method is not correctly implemented. It should be like this:

    public void printGraph(Graph G){
    System.out.println("Number of vertices in the graph : "+G.numVertices);
    for(int i=0;i<numEdges;i++){
    Edge.printEdge(e);
      }
    }
    
  3. In addEdge() method, you need to add this also, otherwise edgeNumber will always be -1

    e1.edgeNumber = edgeIndex;
    
  4. In class Vertex you have to override toString() method, otherwise you will not be able to print the vertex's information in printEdge()correctly.

    @Override
    public String toString() {
    return "Vertex [vertexId=" + vertexId + ", name=" + name + "]";
    }
    
Sign up to request clarification or add additional context in comments.

Comments

0

make your printEdge static:

public static void printEdge(Edge e){
    System.out.println("Edge Number :"+e.edgeNumber);
    System.out.println(e.vertex1);
    System.out.println(e.vertex2);
    System.out.println(e.name);
    System.out.println(e.weight);
}

change you printGraph method:

public void printGraph() {
    System.out.println("Number of vertices in the graph : " + numVertices);
    for (int i = 0; i < numEdges; i++) {
       Edge.printEdge(edgeList[i]);
    }
}

call it like this: g1.printGraph();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.