Can I directly add constraint to the variables to make it impossible to create a new object, if the new object's attributes must meet certain condition. The following part, I wish automatically check that lowerbound <=upperbound. or other something like int lowerBound is larger that 0. In Database, we have check constraint. So I wonder in Java:
import java.math.*;
public class SumAverageRunningInt {
private int lowerBound;
private int upperBound;
public SumAverageRunningInt(int lowerBound, int upperBound) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
public int getlowerBound() {return lowerBound;}
public int getupperBound() {return upperBound;}
public int sum() {
int sum = 0;
int i = 0;
int a = this.getlowerBound();
int b = this.getupperBound();
while (a<=b) {
sum = a + sum;
a = a + 1;}
return sum;
}
}