2017-05-05 221 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