如何在Java中检查一个点的参数是否为NaN。

huangapple go评论64阅读模式
英文:

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 &amp;&amp; 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) &lt; THRESHOLD &amp;&amp; Math.abs(y - ((Point) p1).y) &lt; THRESHOLD);}
		return false;
	}
    
	//toString
	public String toString() {
		return &quot;x=&quot; + x + &quot; y=&quot; + 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 &amp;&amp; 
    			((Rectangle)r1).topLeft == this.topLeft &amp;&amp;
    			r1.getClass() == getClass()) {return true;}
		return false;
    }
        
    //toString
    public String toString() {
		return &quot;topLeft: &quot; + topLeft + &quot; bottomRight&quot; + 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(&quot;Point contains non-numeric coordinate&quot;); 
    } 
    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.

huangapple
  • 本文由 发表于 2020年9月23日 10:32:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64020202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定