/* An instance of this class represents an interval on the set of real ** numbers. For example, [2.4, 23.5] denotes the closed interval with ** lower and upper bounds 2.4 and 23.5, respectively, which is the set ** of real numbers x satisfying the condition 2.4 <= x <= 23.5. ** The corresponding open interval (2.4, 23.5) (notice the use of ** parentheses rather than square brackets) is the same set of numbers, ** except not including the two bounds. An interval can be closed at ** one end and open on the other, as in [2.4, 23.5) or (2.4, 23.5]. ** ** In order to keep things simple, this class treats an interval as being ** closed at each end, unless it is being "subtracted" from another interval. ** In this circumstance, the interval is treated as being open at each end, ** except at any end that is the same as the corresponding end of the ** interval from which it is being subtracted. (These rules ensure that ** any interval produced by the subtract() method is closed at both ends ** and has non-zero length.) ** ** Authors: R. McCloskey and < STUDENT's name > ** CMPS 144 < semester, prog. assg. id > ** Collaborated with: ... ** Known defects: ... */ public class Interval { // instance variables // ------------------ private double lowerBound, upperBound; // lower and upper bounds of this // interval // constructor // ----------- /* Initializes this interval to have the specified bounds. ** If lower > upper, an IllegalArgmentException is thrown, ** with an accompanying message explaining the reason. */ public Interval(double lower, double upper) { if (lower > upper) { throw new IllegalArgumentException("lower > upper"); } lowerBound = lower; upperBound = upper; } // observers // --------- /* Returns the lower bound of this interval. */ public double lowerBound() { return 0; } // STUB! /* Returns the upper bound of this interval. */ public double upperBound() { return 0; } // STUB! /* Returns the length of this interval, which is simply the distance ** between its lower and upper bounds. */ public double length() { return upperBound() - lowerBound(); } /* Reports whether or not the specified value lies in this interval */ public boolean isMemberOf(double x) { return false; // STUB! } /* Reports whether or not the specified object (v) is equal to this ** interval, which requires that v be an instance of Interval and ** that v's lower and upper bounds match those of this interval. */ @Override public boolean equals(Object v) { boolean result; if (v instanceof Interval) { Interval vv = (Interval)v; result = this.lowerBound() == vv.lowerBound() && this.upperBound() == vv.upperBound(); } else { result = false; } return result; } /* Reports whether or not the specified interval (v) is wholly contained ** within (i.e., is a subset of) this interval. */ public boolean contains(Interval v) { return false; // STUB! } /* Reports whether or not this interval is wholly contained within ** (i.e., is a subset of) the specified interval (v) */ public boolean isContainedWithin(Interval v) { return false; // STUB! } /* Reports whether or not the intersection of this interval and the ** specified interval (v) is nonempty. */ public boolean overlaps(Interval v) { return false; // STUB! } @Override /* Returns a string describing this interval, exemplified by "[3.5,7.9]". */ public String toString() { return "[" + lowerBound() + "," + upperBound() + "]"; } // generators // ---------- /* Returns the interval obtained by taking the union of this interval ** and the specified interval (v). ** pre: overlaps(v) */ public Interval union(Interval v) { return new Interval(0.0, 0.0); // STUB! } /* Same as method above, except that an IllegalArgumentException is ** thrown if the above method's precondition is not met. */ public Interval unionSafe(Interval v) { if (!overlaps(v)) { throw new IllegalArgumentException(errorMessage(v)); } return union(v); } /* Returns the interval obtained by taking the intersection of this ** interval and the specified interval (v). ** pre: overlaps(v) */ public Interval intersection(Interval v) { return new Interval(0.0, 0.0); // STUB! } /* Same as method above, except that an IllegalArgumentException is ** thrown if the above method's precondition is not met. */ public Interval intersectionSafe(Interval v) { return new Interval(0.0, 0.0); // STUB! } /* Returns an array containing the interval(s) that result from ** subtracting the specified interval (v) from this one. (Each end ** of the specified interval is interpreted as being open, unless the ** bound is the same as the corresponding bound of this interval. ** This is to ensure that any interval produced by this method is ** closed at both ends and has non-zero length.) ** pre: overlaps(v) && v.length() > 0 ** ** Examples: ** Subtracting (2,7) from [0,5] yields [0,2]. ** Subtracting [0,3) from [0,5] yields [3,5]. ** Subtracting (2,7) from [4,6] yields no interval. ** Subtracting (2,7) from [0,10] yields the two intervals [0,2] and [7,10]. ** ** Note #1: In the second example, the interval being subtracted is ** interpreted to be closed at the lower end because its lower bound ** matches that of the other interval. (Otherwise, the result would ** have included the zero-length interval [0,0].) ** ** Note #2: In the first two examples, an array of length one is returned. ** In the third example, an array of length zero is returned. ** In the fourth, an array of length two is returned. */ public Interval[] subtract(Interval v) { // Note that the length of the array result[] cannot be known until // a comparison between this Interval and Interval v has been carried // out so as to determine whether the length should be zero, one, or // two. Interval[] result = null; // // STUB! // return result; } /* Same as method above, except that an IllegalArgumentException is ** thrown if the above method's precondition is not met. */ public Interval[] subtractSafe(Interval v) { //RWM: The lone line of code here is purely for the purpose of making // this method have a legal method body. That line of code should // not appear in the completed version of this method. return new Interval[0]; // STUB! } // private methods // --------------- /* Returns a string reporting that this Interval and the specified one ** do not overlap. */ private String errorMessage(Interval v) { return this + " and " + v + " do not overlap."; } }