java - Design-by-contract finding pre-conditions -
i have create calculator in java, based on interface.
public interface calculatorif { int add(int x, int y); int sub(int x, int y); int mult(int x, int y); //double div(int x, int y); //int sqrt(int x); } but every method, need pre-post conditions. need pre-conditions, because can't imagine single 1 makes sense , isn't handled java.
edit: division , sqrt clear me, need ideas add, sub , mult.
if add 2 integer.max_value values, result not fit int , truncated. on other hand, if input domain restricted, can guarantee result not truncated , has expected value instead.
for example, if x <= integer.max_value / 2 , y <= integer.max_value / 2, sum x + y less or equal integer.max_value, there no truncation positive integers. similar reasoning can used negative values , integer.min_value. preconditions subtraction can done same way.
for multiplication, if either operand absolute value less sqrt (integer.max_value), product inside range of int.
more sophisticated ways detect overflow , underflow possible, class exercise such preconditions seem fine.
Comments
Post a Comment