### Question:
Range implements Serializable { public double constrain(double value) { double result = value; if (!contains(value)) { if (value > this.upper) { result = this.upper; } else if (value < this.lower) { result = this.lower; } } return result; } Range(double lower, double upper); double getLowerBound(); double getUpperBound(); double getLength(); double getCentralValue(); boolean contains(double value); boolean intersects(double b0, double b1); boolean intersects(Range range); double constrain(double value); static Range combine(Range range1, Range range2); static Range combineIgnoringNaN(Range range1, Range range2); static Range expandToInclude(Range range, double value); static Range expand(Range range,
                               double lowerMargin, double upperMargin); static Range shift(Range base, double delta); static Range shift(Range base, double delta,
                              boolean allowZeroCrossing); static Range scale(Range base, double factor); @Override boolean equals(Object obj); boolean isNaNRange(); @Override int hashCode(); @Override String toString();  }### Answer:
@Test public void testConstrain() { Range r1 = new Range(0.0, 1.0); double d = r1.constrain(0.5); assertEquals(0.5, d, 0.0000001); d = r1.constrain(0.0); assertEquals(0.0, d, 0.0000001); d = r1.constrain(1.0); assertEquals(1.0, d, 0.0000001); d = r1.constrain(-1.0); assertEquals(0.0, d, 0.0000001); d = r1.constrain(2.0); assertEquals(1.0, d, 0.0000001); d = r1.constrain(Double.POSITIVE_INFINITY); assertEquals(1.0, d, 0.0000001); d = r1.constrain(Double.NEGATIVE_INFINITY); assertEquals(0.0, d, 0.0000001); d = r1.constrain(Double.NaN); assertTrue(Double.isNaN(d)); }