2014-09-30 55 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