2017-05-05 219 views
1

我很難拋出IllegalStateForMatrixException。如果有人能幫助我,將不勝感激。這是我的教授告訴我要做的事情,但我仍然遇到問題。這是他給我的一個提示...投擲異常問題

調用帶有0邊的getNumberOfEdges時應引發IllegalStateForMatrixException。 調用帶有0邊的getNumberOfEdges時,應引發IllegalStateForMatrix。 所有你需要做的是檢查矩陣沒有正值。如果它不拋出IllegalStateForMatrix異常。

您還可以將布爾值設置爲false,並在您調用createMatrix時將其設置爲true。這將是一種檢查createMatrix是在addEdge之前還是之後調用的方法。然後檢查addEdge,你可以拋出異常。

class AdjacencyGraph extends Graph { 

private ArrayList nodes = new ArrayList(); 
private int noEdges = 0; 

/** 
* Adjacency matrix. 
*/ 
public int[][] adjM; 

/** 
* Boolean set to false, should be set true in create matrix. 
*/ 
private boolean b = false; 

/** 
* Adjacency Matrix with integers. 
*/ 
@Override 
void createAdjacencyMatrix() { 
    adjM = new int[nodes.size()][nodes.size()]; 
    int i; 
    int j; 
    for (i = 0; i < nodes.size(); i++) { 
     for (j = 0; j < nodes.size(); j++) { 
      adjM[i][j] = -1; 
     } 
     b = true; 
    } 
} 

/** 
* Adds the node. 
* 
* @param nodeName 
*/ 
@Override 
@SuppressWarnings("unchecked") 
void addNode(String nodeName) { 
    nodes.add(nodeName); 
} 

/** 
* Adds the edge. 
* 
* @param fromNode 
* @param toNode 
* @param weight 
* @throws ElementNotFoundException 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
void addEdge(String fromNode, String toNode, int weight) 
     throws ElementNotFoundException, IllegalStateForMatrixException { 
    { 
     try { 
      if (b == false) 
       throw new IllegalStateForMatrixException("Exception"); 
      int i; 
      int j; 
      for (i = 0; i < nodes.size(); i++) { 
       if (nodes.get(i).equals(fromNode)) { 
        break; 
       } 
      } 
      if (i == nodes.size()) { 
       throw new ElementNotFoundException("Exception"); 
      } 

      for (j = 0; j < nodes.size(); j++) { 
       if (nodes.get(j).equals(toNode)) { 
        break; 
       } 
      } 
      if (j == nodes.size()) { 
       throw new ElementNotFoundException("Exception"); 
      } 

      adjM[i][j] = weight; 
      adjM[j][i] = weight; 
      noEdges++; 
     } 
     catch (ElementNotFoundException e) { 
      System.out.println("Exception found"); 
     } 
     catch (IllegalStateForMatrixException e) { 
      System.out.println("Exception found"); 
     } 
    } 
} 

/** 
* Returns the number of nodes. 
* 
* @return number of nodes 
*/ 
@Override 
int getNumberOfNodes() { 
    return nodes.size(); 
} 

/** 
* Returns the number of edges 
* 
* @return number of edges 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
int getNumberOfEdges() throws IllegalStateForMatrixException { 
    if (nodes.size() <= 0) 
     throw new IllegalStateForMatrixException("Error"); 
    return noEdges; 
} 

/** 
* Returns the highest degree node. 
* 
* @return highest degree node. 
* @throws ElementNotFoundException 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
public String getHighestDegreeNode() 
     throws ElementNotFoundException, IllegalStateForMatrixException { 

    int i; 
    int ansIndex = 0; 
    int j; 
    int ansCount = 0; 
    for (i = 0; i < nodes.size(); i++) { 
     int k = 0; 
     for (j = 0; j < nodes.size(); j++) { 
      if (adjM[i][j] != -1) { 
       k++; 
      } 
     } 
     if (k > ansCount) { 
      ansCount = k; 
      ansIndex = i; 
     } 
    } 
    return (String) nodes.get(ansIndex); 
} 

/** 
* Cost of the edge between nodes. 
* 
* @param fromNode 
* @param toNode 
* @return returns -1 
* @throws ElementNotFoundException 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
int costOfEdgeBetween(String fromNode, String toNode) 
     throws ElementNotFoundException, IllegalStateForMatrixException { 
    try { 
     int i; 
     int j; 
     for (i = 0; i < nodes.size(); i++) { 
      if (nodes.get(i).equals(fromNode)) { 
       break; 
      } 
     } 
     if (i == nodes.size()) { 
      throw new ElementNotFoundException("Exception"); 
     } 

     for (j = 0; j < nodes.size(); j++) { 
      if (nodes.get(j).equals(toNode)) { 
       break; 
      } 
     } 
     if (j == nodes.size()) { 
      throw new ElementNotFoundException("Exception"); 
     } 

     return adjM[i][j]; 
    } 
    catch (ElementNotFoundException e) { 
     System.out.println("Exception found"); 
    } 
    return -1; 
} 

/** 
* 
* @param fromName 
* @param toName 
* @return false 
* @throws ElementNotFoundException 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
boolean hasPathBetween(String fromName, String toName) 
     throws ElementNotFoundException, IllegalStateForMatrixException { 
    try { 
     ArrayStack<Integer> st = new ArrayStack<Integer>(); 
     int[] visited = new int[nodes.size()]; 
     int i; 
     int j; 
     int start; 
     int end; 
     for (i = 0; i < nodes.size(); i++) { 
      visited[i] = 0; 
     } 

     for (i = 0; i < nodes.size(); i++) { 
      if (nodes.get(i).equals(fromName)) { 
       break; 
      } 
     } 
     start = i; 

     for (j = 0; j < nodes.size(); j++) { 
      if (nodes.get(j).equals(toName)) { 
       break; 
      } 
     } 
     end = j; 
     st.push(start); 
     visited[start] = 1; 
     while (st.isEmpty() != true) { 
      i = st.pop(); 
      if (i == end) { 
       return true; 
      } 
      for (j = 0; j < nodes.size(); j++) { 
       if (adjM[i][j] != -1 && visited[j] == 0) { 
        visited[j] = 1; 
        st.push(j); 
       } 
      } 
     } 
     return false; 
    } 
    catch (Exception e) { 
     System.out.println("Exception found"); 
    } 
    return false; 
} 

/** 
* The number of Isolated points 
* 
* @return ans 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
int numIsolatedPoints() throws IllegalStateForMatrixException { 
    int i; 
    int j; 
    int ans = 0; 
    for (i = 0; i < nodes.size(); i++) { 
     for (j = 0; j < nodes.size(); j++) { 
      if (adjM[i][j] != -1) { 
       break; 
      } 
     } 
     if (j == nodes.size()) { 
      ans++; 
     } 
    } 
    return ans; 
} 

/** 
* Inclusiveness is the percentage of points in the graph that are not 
* isolated 
* 
* @return ((float)i)/nodes.size() 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
float inclusiveness() throws IllegalStateForMatrixException { 
    int i = nodes.size() - numIsolatedPoints(); 
    return ((float) i)/nodes.size(); 
} 

/** 
* Density of matrix 
* 
* @return (2*(float)noEdges)/(nodes.size()*(nodes.size()-1)) 
* @throws IllegalStateForMatrixException 
*/ 
@Override 
float density() throws IllegalStateForMatrixException { 
    return (2 * (float) noEdges)/(nodes.size() * (nodes.size() - 1)); 
} 

/** 
* Print out the adjacency matrix 
*/ 
void print() { 
    System.out.println(adjM); 
} 

}

+0

您正在捕捉異常,然後才能退出您的方法。或者你的實際問題是什麼? –

+0

歡迎來到SO。一些建議,閱讀[問],發佈[mcve],並解釋「有問題」的含義。謝謝 – OldProgrammer

+0

你的問題怎麼樣?你想要的方式是什麼? – victor

回答

0

我怎麼扔,並適當地捕捉IllegalStateForMatrix。 哪裏能正確捕捉異常? -

這裏IllegalStateForMatrixException表示在對象未處於具有所需前置條件的狀態下接受此操作時調用該對象的操作。

你想得到一些邊緣,但你沒有矩陣? 它沒有任何意義。

這是一個重要的問題,所以你必須提出異常,直到引發異常的方法的調用者上升

除了這些方法聲明投擲IllegalStateForMatrixException
因此,如果檢查異常(不是RuntimeException的子節點),則客戶端必須處理它。

在你的代碼中,有兩種方法拋出異常。
在這裏,你做好的事情:

@Override 
int getNumberOfEdges() throws IllegalStateForMatrixException { 
    if (nodes.size() <= 0) 
     throw new IllegalStateForMatrixException("Error"); 
    return noEdges; 
} 

你拋出異常到具有處理它的客戶端。

卜有:

void addEdge(String fromNode, String toNode, int weight) 
     throws ElementNotFoundException, IllegalStateForMatrixException {   
    try { 
     if (b == false) 
      throw new IllegalStateForMatrixException("Exception"); 
     } 
     ... 
     } 
    catch (IllegalStateForMatrixException e) { 
     System.out.println("Exception found"); 
    } 
     ... 
} 

你不允許異常傳播到主叫方,你超越它的方法,但你也趕上它在
正如在第一種情況下,你。不需要有try/catch語句。

+0

我會擺脫ElementNotFoundException的try catch嗎?只要扔他們沒有趕上...感謝您的幫助順便說一句。 – buck4life

+0

歡迎您:)對於IllegalStateForMatrixException,這是一樣的。如果你在它已經上升的地方捕獲它,那麼明確拋出ElementNotFoundException的意義是什麼? – davidxxx