英文:
How to check a Point don't have NaN as parameter in java
问题
以下是您提供的内容的中文翻译部分:
在Java中,我创建了一个点(Point)类:
public class Point {
private final float THRESHOLD = (float) 0.0001;
private final float x;
private final float y;
//getter
public float getX(){return x;}
public float getY(){return y;}
//constructor
public Point(float x,float y){this.x = x;this.y = y;}
//equals
public boolean equals(Object p1) {
if (p1 != null && p1.getClass() == getClass()) {return (Float.floatToIntBits(x) == Float.floatToIntBits(((Point) p1).x))
||(Float.floatToIntBits(y) == Float.floatToIntBits(((Point) p1).y)
||Math.abs(x - ((Point) p1).x) < THRESHOLD && Math.abs(y - ((Point) p1).y) < THRESHOLD);}
return false;
}
//toString
public String toString() {
return "x=" + x + " y=" + y;
}
}
然后,我使用两个点作为左上角(topLeft)和右下角(bottomRight)创建了一个矩形(Rectangle)类:
public final class Rectangle {
private final Point topLeft;
private final Point bottomRight;
//getters
public Point getTopLeft() {return topLeft;}
public Point getBottomRight() {return bottomRight;}
//constructor
Rectangle(Point topLeft, Point bottomRight){this.topLeft = topLeft; this.bottomRight = bottomRight;}
Rectangle(float topLeftX, float topLeftY, float bottomRightX, float bottomRightY){
this(new Point(topLeftX, topLeftY), new Point(bottomRightX, bottomRightY));
}
//equals
public boolean equals(Object r1) {
if (((Rectangle)r1).bottomRight == this.bottomRight &&
((Rectangle)r1).topLeft == this.topLeft &&
r1.getClass() == getClass()) {return true;}
return false;
}
//toString
public String toString() {
return "topLeft: " + topLeft + " bottomRight" + bottomRight;
}
//computeArea
public float computeArea() {
return Math.abs(
(topLeft.getX() - bottomRight.getX()) * (bottomRight.getY() - topLeft.getY()));
}
}
然而,正如我们所知,当点的某个参数等于NaN时,矩形类应该不起作用(比如抛出错误),如何设计它呢?谢谢。
英文:
I created a point class in Java:
public class Point {
private final float THRESHOLD = (float) 0.0001;
private final float x;
private final float y;
//getter
public float getX(){return x;}
public float getY(){return y;}
//constructor
public Point(float x,float y){this.x = x;this.y = y;}
//equals
public boolean equals(Object p1) {
if (p1 != null && p1.getClass() == getClass()) {return (Float.floatToIntBits(x) == Float.floatToIntBits(((Point) p1).x))
||(Float.floatToIntBits(y) == Float.floatToIntBits(((Point) p1).y)
||Math.abs(x - ((Point) p1).x) < THRESHOLD && Math.abs(y - ((Point) p1).y) < THRESHOLD);}
return false;
}
//toString
public String toString() {
return "x=" + x + " y=" + y;
}
}
Then I created a rectangle class using the two points as topLeft and bottomright:
public final class Rectangle {
private final Point topLeft;
private final Point bottomRight;
//getters
public Point gettopLeft() {return topLeft;}
public Point getbottomRight() {return bottomRight;}
//constructor
Rectangle(Point topLeft,Point bottomRight){this.topLeft = topLeft; this.bottomRight = bottomRight;}
Rectangle(float topLeftX,float topLeftY,float bottomRightX, float bottomRightY){
this(new Point(topLeftX,topLeftY),new Point(bottomRightX,bottomRightY));
}
//equals
public boolean equals(Object r1) {
if (((Rectangle)r1).bottomRight == this.bottomRight &&
((Rectangle)r1).topLeft == this.topLeft &&
r1.getClass() == getClass()) {return true;}
return false;
}
//toString
public String toString() {
return "topLeft: " + topLeft + " bottomRight" + bottomRight;
}
//computeArea
public float computeArea() {
return Math.abs(
(topLeft.getX() - bottomRight.getX())*(bottomRight.getY() - topLeft.getY()));
}
}
However, we all know when one parameter in point equal to NaN, the rectange should not work(like throw errors), how to design it? Thanks.
答案1
得分: 2
你可以使用 Float.isNan
方法:
Rectangle(Point topLeft, Point bottomRight){
if (Float.isNan(topLeft.getX()) || Float.isNan(topLeft.getY()) ||
Float.isNan(bottomRight.getX()) || Float.isNan(bottomRight.getY())) {
throw new IllegalArgumentException("Point contains non-numeric coordinate");
}
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
或者,你可以将这个检查封装在 Point
类中,通过添加一个方法,只有在两个坐标都不是 NaN 时才返回 true
。然后你可以在 Rectangle
构造函数中对两个点都调用该方法。
英文:
You can use the Float.isNan
method:
Rectangle(Point topLeft, Point bottomRight){
if (Float.isNan(topLeft.getX()) || Float.isNan(topLeft.getY()) ||
Float.isNan(bottomRight.getX()) || Float.isNan(bottomRight.getY())) {
throw new IllegalArgumentException("Point contains non-numeric coordinate");
}
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
Alternatively, you can encapsulate this check in the Point
class by adding a method that will only return true
if both coordinates are not NaN. You would then be able to call that method on both points in the Rectangle
constructor.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论