FileDocCategorySizeDatePackage
RangedInt.javaAPI DocExample1866Sat Jan 27 21:50:32 GMT 2001None

RangedInt

public class RangedInt extends Object
A RangedInt is similar to Integer but enforces upper and lower bounds that are set at construction time. Originally for Learning Tree International Course 471.

Fields Summary
protected int
currentValue
Current value
protected int
maxValue
Maximum value allowed
protected int
minValue
Minimum value allowed
Constructors Summary
public RangedInt(int min, int max)
Constructor: Stores the maximum and minimum values and sets the current value to 0 or the minimum value whichever is greater.

            // store the supplied limits
            minValue = min;
            maxValue = max;

            // set the initial value of this object
            if (min > 0)
                currentValue = min;
            else
                currentValue = 0;
        
public RangedInt()
Default Constructor: Sets the maximum and minimum values to MAX_VALUE and MIN_VALUE.

            // call the (int, int) constructor
            // with the max and min values
            this(Integer.MIN_VALUE, Integer.MAX_VALUE);
        
Methods Summary
public intgetValue()
Return the current value of the object

            // return the current value
            return currentValue;
        
public voidsetValue(int newValue)
Set the current value of the object. If new value is outside bounds then throw an IllegalArgumentException


            // check the new value
            if (newValue < minValue || newValue > maxValue)
                throw new IllegalArgumentException("From RangedInt");
			currentValue = newValue;