i cannot find a reliable source online that explains the proper syntax and my instructor's help is negligible. Please any insight is greatly appreciated. This is my last study guide question for my final later today! the problem is identified with arrows below. i was provided source code to a similar problem where this add seems to be working just fine that can be found below.

``````public Iterable<Integer> adj(int v)  {   //vertices adjacent to v

for(int i = 0; i < v; i++){
for(int j = 0; j < i; j++){
if(matrix[i][j]){
}
}
}
}

package edu.csus.csc130.assignment4;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Scanner;

//*********** PROVIDED: *******************

/**
* This Graph implementation maintains a vertex-indexed array of lists of integers.
* Every edge appears twice: if an edge connects v and w, then w appears in v’s list and v appears in w’s list.
*/
public class Graph {
private int V = 0; // number of vertices
private int E; // number of edges

//create a V-vertex graph with no edges
@SuppressWarnings("unchecked")
public Graph(int V)  {
this.V = V;
this.E = 0;
// Initialize all lists to empty
for (int v = 0; v < V; v++) {
}
}

/**
* reads a graph from an input file, in the format V followed by E followed by a list of
* pairs of int values between 0 and V-1.
*/
@SuppressWarnings("unchecked")
public Graph(String inFileName) {
Scanner sc = null;
try {
URL url = Graph.class.getResource(inFileName);
sc = new Scanner(new File(url.getPath()));
int numVertices = sc.nextInt();
int numEdges = sc.nextInt();

this.V = numVertices;
// Initialize all lists to empty
for (int v = 0; v < V; v++) {
}

for (int i=0; i<numEdges; i++) {
int vertexA = sc.nextInt();
int vertexB = sc.nextInt();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sc != null) {
sc.close();
}
}
}

//return number of vertices
public int V() {
return V;
}

//return number of edges
public int E() {
return E;
}

//add edge v-w to this graph
public void addEdge(int v, int w) {
E++;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(V + " vertices, " + E + " edges\n");
for (int v = 0; v < V; v++) {
sb.append(v + ": ");
for (int w : this.adj(v)) {
sb.append(w + " ");
}
sb.append("\n");
}
return sb.toString();
}

}

@SuppressWarnings("unused")
public static int degree(Graph G, int v) {
int degree = 0;
for (int w : G.adj(v)) {
degree++;
}
return degree;
}

public static int maxDegree(Graph G) {
int max = 0;
for (int v = 0; v < G.V(); v++) {
int degree = degree(G, v);
if (degree > max) max = degree;
}
return max;
}

public static int avgDegree(Graph G) {
return 2 * G.E() / G.V();
}

public static int numberOfSelfLoops(Graph G) {
int count = 0;
for (int v = 0; v < G.V(); v++)
if (v == w)
count++;
return count / 2;
}

}
``````
2
Contributors
3
Replies
20
Views
1 Year
Discussion Span
Last Post by JamesCherrill

Having a method and a data member with the same name `adj` is bound to lead to confusion.

Edited by JamesCherrill

i agree, does that seem to be the problem here? i have just tried changing the list name but same issue continues.

Line 2: you define adj (or whatever) as an Iterable<etc>. Later you assign a LinkedList to that. That's OK because LinkedList implements Iterable. However later on you try to call add(etc), which you know is OK because you know it's called on a LinkedList. But all the compiler knows is that you call it on an Iterable, and Iterable does not include an add method.