Skip to content
Advertisement

How to check a Point don’t have NaN as parameter in java

I created a point class in Java:

JavaScript

Then I created a rectangle class using the two points as topLeft and bottomright:

JavaScript

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.

Advertisement

Answer

You can use the Float.isNan method:

JavaScript

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement