. . . this sounds like a "Design by Contract" [DBC] problem. DBC appears to be a trademark of the Eiffel language; it is discussed
here. What it means is that a method is allowed to check the validity of any arguments passed to it. If the arguments fail the validity check (precondition), then the called method throws an Exception. Look up, for example, the java.awt.Color(int int int) constructors. If you pass the wrong values for the three numbers, the constructor throws an Exception. That means, if there is an error, then the method calling has the error, and it is held responsible for sorting it out. Also in the following Circle class, I have put a precondition in the constructor; you can't have a negative radius.
ALSO: there may be a postcondition. That is something which has to be true at the end of the method. You can have an assertion like in this method, in the Circle class which I have just thought up:-
Now there is an assertion at the end of the setArea method, that area equals PI*r^2.
The idea of preconditions and postconditions was set out by Hoare in the 1960s; if the precondition is fulfilled, a "correct" method must thereafter fulfil its postcondition in all instances.
Note it was not necessary to check precondition and postcondition in every method. If there is a violation of the postcondition, the method needs to be checked on the assumption there is an error in its code.
There is an additional feature which might need to be validated, called the class invariant. In the case of the circle, there are actually two invariants:-
Area equals PI * radius ^ 2, andCircumference equals 2 * PI * radius. A more stringent implementation of Circle would calculate the circumference and area in the constructor, and have an invariant method, which can be called in every other method:-
Now that class has an invariant at its start, and finish. It has a precondition, and the postcopndition is implicit, being incorporated in the invariant.
That will give a rigorous check of the Circle class, confirming "correctness" at all points, so you can confirm it works properly.
You can enable assert statements with the -ea flag on the JVM, so you can run it in a production version missing out the assertions, with faster processing.
CR
PS: Beware of floating point arithmetic. All will be well until you try the square root of 400 and get 19.999999999999999