2014-09-30 73 views
-1

嘿,我有一個點類,它會生成一個點列表。該類如下沒有引發異常消息

public class Point { 

private double x, y; 



public Point(double x, double y){ 

    this.x = x; 
    this.y = y; 

}//end constructor 




public Point() { 
    // TODO Auto-generated constructor stub 
} 

public double getX(){ 
     return x; 
} 

public void setX(double x) { 
    try{ 
     if (x < 0){ 
      throw new NegArgumentException(); 
     }//end if 
     else{ 
      this.x = x; 

     }//end else 
    }//end try 
    catch (NegArgumentException e){ 
     System.out.println("error"); 
    }//end catch 


} 

public double getY() { 
    return y; 
} 

public void setY(double y) { 
    try{ 
     if (y < 0){ 
      throw new NegArgumentException(); 
     }//end if 
     else{ 
      this.y = y; 

     }//end else 
    }//end try 
    catch (NegArgumentException e){ 
     System.out.println("error"); 
    }//end catch 

} 

public String toString(){ 
    return "p(" + x + "," + y + ")"; 
} 
} 

我也創建了自己的異常類NegArgumentException異常如果生成負面點會引發異常。

public class NegArgumentException extends Exception { 


public NegArgumentException(){} 

public NegArgumentException(String message){ 
    super(message); 
} 

}

我也有其他的2類矩形和圓形

public class Rectangle { 
private Point upperLeft = new Point(); 
private Point lowerRight = new Point(); 


public Rectangle(Point upperLeft, Point lowerRight){ 
    this.upperLeft = upperLeft; 
    this.lowerRight = lowerRight; 

}//end constructor 

public Point getUpperLeft() { 
    return upperLeft; 
} 

public void setUpperLeft(Point upperLeft) { 
    this.upperLeft = upperLeft; 
} 

public Point getLowerRight() { 
    return lowerRight; 
} 

public void setLowerRight(Point lowerRight) { 
    this.lowerRight = lowerRight; 
} 

public Point getUpperRight(){ 
    return new Point(getLowerRight().getX(), getUpperLeft().getY()); 
} 

public Point getLowerLeft(){ 
    return new Point(getUpperLeft().getX(), getLowerRight().getY()); 
} 


public double getArea(){ 
    return upperLeft.getX()*lowerRight.getY(); 
} 

public String toString(){ 
    return "r[("+upperLeft.getX() + ","+upperLeft.getY()+"),("+lowerRight.getX()+","+lowerRight.getY()+")]"; 
} 
} 

和圓類

public class Circle { 
private Point center = new Point(); 
private double radius; 

public Circle(){} 

public Circle(Point center, double radius){ 
    this.center = center; 
    this.radius = radius; 
} 

public Point getCenter() { 
    return center; 
} 

public void setCenter(Point center) { 
    this.center = center; 
} 

public String toString(){ 
    return "c[("+center.getX()+","+center.getY()+"), "+ radius+"]"; 
} 
} 

我的主類生成每個點的隨機列表這些對象。

import java.util.ArrayList; 

import java.util.Random;

公共類主要{

public static void main(String[] args) { 
    //create arraylist for each class 
    ArrayList<Point> list = new ArrayList<Point>(); 
    ArrayList<Rectangle> list1 = new ArrayList<Rectangle>(); 
    ArrayList<Circle> list2 = new ArrayList<Circle>(); 
    //create random generators 
    Random Point = new Random(); 
    Random myGenerator = new Random(); 
    int i = 0; 
    //go through for loop 
    while (i < 100){ 

     int whichArray = myGenerator.nextInt(4) + 1; 
     //print out points for whichever random object 
     if (whichArray == 1){ 
      int pointX = Point.nextInt(41) - 20; 
      int pointY = Point.nextInt(41) - 20; 
      if (pointX >= 0 && pointY >= 0){ 
      list.add(new Point(pointX, pointY)); 
      System.out.println(list.toString()); 
      list.remove(0); 
      i++; 
      } 
     }//end if 
     if (whichArray == 2){ 
      int pointX = Point.nextInt(41) - 20; 
      int pointY = Point.nextInt(41) - 20; 
      int pointRandom = Point.nextInt(20) - 20; 
      if (pointX >= 0 && pointY >= 0 && pointRandom >= 0){ 
      list1.add(new Rectangle(new Point(pointX, pointY), new Point(pointX+pointRandom, pointY-pointRandom))); 
      System.out.println(list1.toString()); 
      list1.remove(0); 
      i++; 
      } 

     }//end if 
     if (whichArray == 3){ 
      int pointX = Point.nextInt(41) - 20; 
      int pointY = Point.nextInt(41) - 20; 
      int radius = Point.nextInt(41) - 20; 
      if (pointX >= 0 && pointY >= 0){ 
      list2.add(new Circle(new Point(pointX, pointY),radius)); 
      System.out.println(list2.toString()); 
      list2.remove(0); 
      i++; 
      } 
     }//end if 

    }//end while loop 

}//end main 


}//end class 

的問題是,我沒有看到在發生異常時顯示的任何消息。我知道這是工作,因爲我沒有得到我的觀點的負面座標。有誰知道爲什麼沒有消息顯示?

+1

何時和爲什麼會發生異常?我看到'如果(pointX> = 0 && pointY> = 0)'守衛到處! – 2014-09-30 22:11:45

+0

好的事情是,當我擺脫這些,任何數字,這將是消極的去0,所以我的隨機座標有更多的0比它應該@ElliottFrisch – Heyya 2014-09-30 22:13:39

+1

爲什麼不只是你想開始的範圍? – 2014-09-30 22:15:10

回答

0

您設置的Point值的構造函數中:

list.add(new Point(pointX, pointY)); 

所以,setX的和SETY永遠不會調用,所以,這是從來沒有,如果X和Y是否定驗證。

嘗試這樣:

public class Main { 
public static void main(String[] args) { 
    //create arraylist for each class 
    ArrayList<Point> list = new ArrayList<Point>(); 
    ArrayList<Rectangle> list1 = new ArrayList<Rectangle>(); 
    ArrayList<Circle> list2 = new ArrayList<Circle>(); 
    //create random generators 
    Random Point = new Random(); 
    Random myGenerator = new Random(); 
    int i = 0; 
    //go through for loop 
    while (i < 100){ 
    int whichArray = myGenerator.nextInt(4) + 1; 
    //print out points for whichever random object 
    if (whichArray == 1){ 
     int pointX = Point.nextInt(41) - 20; 
     int pointY = Point.nextInt(41) - 20; 
     if (pointX >= 0 && pointY >= 0){ 
      Point point = new Point(); 
      point.setX(pointX); 
      point.setY(pointY); 
      list.add(point); 
      System.out.println(list.toString()); 
      list.remove(0); 
      i++; 
     } 
    }//end if 
    if (whichArray == 2){ 
     int pointX = Point.nextInt(41) - 20; 
     int pointY = Point.nextInt(41) - 20; 
     int pointRandom = Point.nextInt(20) - 20; 
     if (pointX >= 0 && pointY >= 0 && pointRandom >= 0){ 
      Point point = new Point(); 
      point.setX(pointX); 
      point.setY(pointY); 

      Point point2 = new Point(); 
      point2.setX(pointX+pointRandom); 
      point2.setY(pointY-pointRandom); 

      list1.add(new Rectangle(point, point2)); 
      System.out.println(list1.toString()); 
      list1.remove(0); 
      i++; 
     } 

    }//end if 
    if (whichArray == 3){ 
     int pointX = Point.nextInt(41) - 20; 
     int pointY = Point.nextInt(41) - 20; 
     int radius = Point.nextInt(41) - 20; 
     if (pointX >= 0 && pointY >= 0){ 
     list2.add(new Circle(new Point(pointX, pointY),radius)); 
     System.out.println(list2.toString()); 
     list2.remove(0); 
     i++; 
     } 
    }//end if 

}//end while loop 

}//end main 


}//end class 

這種方式,驗證會發生。

所以,如果,例如,將其更改爲:

Point point = new Point(); 
point.setX(-1); 
point.setY(-2); 
list.add(point); 

它會打印:

error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(17.0,10.0), 13.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(20.0,6.0), 5.0]] 
[c[(18.0,1.0), -18.0]] 
[c[(18.0,20.0), 10.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(20.0,19.0), 20.0]] 
[c[(20.0,12.0), -14.0]] 
[c[(2.0,20.0), 0.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(10.0,13.0), 2.0]] 
[c[(10.0,4.0), 15.0]] 
[c[(20.0,7.0), 16.0]] 
[c[(14.0,18.0), 2.0]] 
[c[(20.0,8.0), 17.0]] 
[c[(16.0,15.0), -13.0]] 
[c[(7.0,6.0), 1.0]] 
[c[(11.0,0.0), -15.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(15.0,2.0), 20.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(3.0,5.0), -7.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(16.0,5.0), -10.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(9.0,1.0), -3.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(13.0,17.0), 7.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(7.0,16.0), 14.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(17.0,13.0), -5.0]] 
[c[(14.0,8.0), 6.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(13.0,0.0), 14.0]] 
[c[(1.0,9.0), 2.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(2.0,17.0), -1.0]] 
[c[(17.0,8.0), -6.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(7.0,19.0), -10.0]] 
[c[(20.0,7.0), 3.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(2.0,7.0), -10.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(2.0,9.0), 14.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(5.0,15.0), 18.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(18.0,18.0), -5.0]] 
[c[(6.0,2.0), -15.0]] 
[c[(12.0,4.0), -3.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(16.0,0.0), 20.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(11.0,12.0), -19.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(2.0,13.0), 13.0]] 
[c[(20.0,2.0), 11.0]] 
[c[(0.0,6.0), -10.0]] 
error 
error 
[p(0.0,0.0)] 
[c[(17.0,17.0), 7.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(0.0,16.0), -11.0]] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
error 
error 
[p(0.0,0.0)] 
[c[(14.0,4.0), -2.0]] 
[c[(0.0,3.0), 11.0]] 
[c[(11.0,15.0), -7.0]] 
[c[(19.0,16.0), 19.0]] 
[c[(8.0,13.0), 15.0]] 

編輯的問題:問題是我不能讓0的到位負座標。我需要得到一個正數。

更改Point類方法是:

public void setX(double x) { 
    try{ 
     if (x <= 0){ 
      throw new NegArgumentException(); 
     }//end if 
     else{ 
      this.x = x; 

     }//end else 
    }//end try 
    catch (NegArgumentException e){ 
     System.out.println("error"); 
    }//end catch 

} 

看:X < = 0setY同樣的事情。

編輯再次,以不產生0的,看看下面的代碼:

public class Main { 
public static void main(String[] args) { 
    //create arraylist for each class 
    ArrayList<Point> list = new ArrayList<Point>(); 
    ArrayList<Rectangle> list1 = new ArrayList<Rectangle>(); 
    ArrayList<Circle> list2 = new ArrayList<Circle>(); 
    //create random generators 
    Random randomNum = new Random(); 
    Random myGenerator = new Random(); 
    int i = 0; 
    //go through for loop 
    while (i < 100){ 

     int whichArray = myGenerator.nextInt(4) + 1; 
     //print out points for whichever random object 
     if (whichArray == 1){ 
      int pointX = 0; 
      while (pointX == 0){ 
       pointX= randomNum.nextInt(41) - 20; 
      } 
      int pointY = 0; 
      while(pointY == 0){ 
       pointY = randomNum.nextInt(41) - 20; 
      } 
      if (pointX >= 0 && pointY >= 0){ 
       Point point = new Point(); 
       point.setX(pointX); 
       point.setY(pointY); 
       list.add(point); 
       System.out.println(list.toString()); 
       list.remove(0); 
       i++; 
      } 
     }//end if 
     if (whichArray == 2){ 
      int pointX = 0; 
      while (pointX == 0){ 
       pointX= randomNum.nextInt(41) - 20; 
      } 
      int pointY = 0; 
      while(pointY == 0){ 
       pointY = randomNum.nextInt(41) - 20; 
      } 
      int pointRandom = randomNum.nextInt(20) - 20; 
      if (pointX >= 0 && pointY >= 0 && pointRandom >= 0){ 
       Point point = new Point(); 
       point.setX(pointX); 
       point.setY(pointY); 

       Point point2 = new Point(); 
       point2.setX(pointX+pointRandom); 
       point2.setY(pointY-pointRandom); 

       list1.add(new Rectangle(point, point2)); 
       System.out.println(list1.toString()); 
       list1.remove(0); 
       i++; 
      } 

     }//end if 
     if (whichArray == 3){ 
      int pointX = 0; 
      while (pointX == 0){ 
       pointX= randomNum.nextInt(41) - 20; 
      } 
      int pointY = 0; 
      while(pointY == 0){ 
       pointY = randomNum.nextInt(41) - 20; 
      } 
      int radius = randomNum.nextInt(41) - 20; 
      if (pointX >= 0 && pointY >= 0){ 
      list2.add(new Circle(new Point(pointX, pointY),radius)); 
      System.out.println(list2.toString()); 
      list2.remove(0); 
      i++; 
      } 
     }//end if 

    }//end while loop 

}//end main 


}//end class 

雖然是0,我會繼續產生另一個隨機數。

+0

當我按照您的方法進行操作時,它表示該方法對於隨機類型未定義。當我嘗試投射它時,它說不能投射。 – Heyya 2014-09-30 22:24:13

+0

我改變了完整的代碼,所以你可以試試 – 2014-09-30 22:25:38

+0

問題是我不能取0代替負座標。我需要得到一個正數。 – Heyya 2014-09-30 22:34:38

3

您的代碼中存在太多不好的做法,您應該閱讀有關編寫乾淨代碼的內容。順便說一下,不會拋出異常,因爲你的setter永遠不會被調用。你的構造函數改爲

public Point(double x, double y) {  
    setX(x); 
    setY(y); 
} 

另外,你始終確保座標是正的創建點之前,您if報表,所以沒有辦法一個無效Point可以實例化。

最後,即使它與你的「問題」沒有任何關係,也不要給一個大寫字母開頭的變量(你的Random對象),這對於可讀性來說是一個壞習慣,甚至更糟它也是一個班的名字。 Point.method()可能含糊不清,如果method存在於類Point和類Random