嘿,我有一个点类,可以生成一个点列表。该课程如下
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);
}
}
我还有另外两个类Rectangle和circle
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()+")]";
}
}
Circle类
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 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){
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
问题是我没有看到发生异常时显示的任何消息。我知道它有效,因为我没有得到负点坐标。有谁知道为什么没有显示消息?
答案 0 :(得分:3)
您的代码中有很多不良做法,您应该阅读有关编写干净代码的内容。顺便说一下,没有异常被抛出,因为你的setter永远不会被调用。将构造函数更改为
public Point(double x, double y) {
setX(x);
setY(y);
}
另外,在创建积分之前,始终确保您的if
语句的坐标为正,因此无法实例化无效的Point
。
最后,即使它与你的“问题”无关,也不要给变量(你的Random
对象)一个以大写字母开头的名字,这对于可读性来说是一个坏习惯。更糟糕的是它也是一个类的名字。如果Point.method()
课程method
和课程Point
中都存在Random
,则{{1}}可能会有歧义。
答案 1 :(得分: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 <= 0 。 setY
也是如此。
再次编辑,要不生成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时,我将继续生成另一个随机数。