Create a class to model intervals of the real line. Each interval has a minimum and a maximum, and contains
all of the real numbers between its minimum and its maximum.
(This kind of object has applications in
geometry, scheduling, and text editing.)
For example, the interval [3, 6] from 3 to 6 contains 3, π, 5, and 6, but it does not contain 6.21.
(All intervals we deal with will include both endpoints.)
Your code must produce the following API:
public Interval(double lo, double hi) // construct interval from lo to hi; but
// throw a RuntimeException if lo > hi
String toString() // return text representation, e.g. "[3, 6]"
boolean contains(double x) // does this interval contain x?
boolean intersects(Interval other) // do these intervals contain a common point?
boolean subsetOf(Interval other) // is this interval a subset of the other?
boolean supersetOf(Interval other) // is this interval a superset of the other?
For
subsetOf, the containment doesn't have to be strict. For example
[3, 4] is a subset of [3, 4.5], and every interval is a subset of itself.