/* Author : Deepak Majeti Date : 8/10/2008 Program: LAB7_Mon, Interval.java Report bugs to: mdeepak@iitk.ac.in */ /* A one dimensional open interval (ℓ, r) for real numbers ℓ, r with ℓ ≤ r is the set of all those points p on the real line such that ℓ < p < r. Here we call ℓ as the left endpoint and r as the right endpoint of the interval (ℓ, r). An interval (ℓ, r) is called empty if ℓ = r. It can be seen that an empty interval does not contain any point of the real line. Design and implement a class Interval with the following attributes and methods. Attributes : double left; double right; Constructors : • Interval(double ℓ, double r) It should build an interval with left endpoint = min(ℓ, r) and right endpoint = max(ℓ, r). • Interval(double ℓ) It should build an interval with left endpoint ℓ and right endpoint ℓ + 1. • Interval( Interval I) It should build a copy of the interval I, that is, left endpoint of the new interval should be the same as the left endpoint of I, and the right endpoint of the new interval should be the same as the right endpoint of I. Methods : (a) public double Length() To determine the length of the current interval. The length of an interval is defined in the usual manner. For example, length of the interval [2.5, 3.8] is 1.3 and length of the interval [−4, 2] is 6. (b) public boolean Isempty() Returns true if the current interval is empty. (c) public boolean Contains( double r) Returns true if the current interval contains the point r of real line, and false oth- erwise. For example, [2, 5.4] contains the point 3.1 but does not contain the point 6. (d) public Interval intersection(Interval I) To return the reference to an interval which is the intersection of the current interval and the interval I. The intersection is defined in the usual manner. For example, intersection of interval (1, 4.3) with the interval (2, 6) is equal to the interval (2, 4.3), whereas Intersetion of interval (1, 4.3) with interval (−4, 0) is an empty interval. We shall follow the convention that if (ℓ1 , r1 ) does not intersects (ℓ2 , r2 ), and r1 ≤ ℓ2 , then the intersection interval is (r1 , r1 ). (e) public double MidPoint() Returns the mid point of the current interval. For example, mid point of the interval (1, 4.4) is 2.7. (f) public double Distance(Interval I) Returns the distance between the current interval and the interval I. The distance between two intervals is equal to the absolute difference between their mid-points. This method must invoke the MidPoint method desribed above. Note that the distance is always non-negative. */ //import java.util.*; class Interval{ //Attributes : double left; double right; //Constructors: /*build an interval with left endpoint = min(ℓ, r) and right endpoint = max(ℓ, r).*/ Interval(double l, double r){ left=Math.min(l,r); right=Math.max(l,r); } // build an interval with left endpoint ℓ and right endpoint ℓ + 1. Interval(double l){ left=l; right=l+1; } /*builds a copy of the interval I, that is, left endpoint of the new interval should be the same as the left endpoint of I, and the right endpoint of the new interval should be the same as the right endpoint of I.*/ Interval(Interval I){ left=I.left; right=I.right; } //Methods: /*To determine the length of the current interval. The length of an interval is defined in the usual manner. For example, length of the interval [2.5, 3.8] is 1.3 and length of the interval [−4, 2] is 6*/ public double Length(){ return Math.abs(left-right); } // Returns true if the current interval is empty. public boolean Isempty(){ return (left==right); } // Returns true if the current interval contains the point r of real line, and false oth- // erwise. For example, [2, 5.4] contains the point 3.1 but does not contain the point 6 public boolean Contains( double r){ if(left<=r&&r<=right) return true; else return false; } // To return the reference to an interval which is the intersection of the current interval // and the interval I. The intersection is defined in the usual manner. For example, // intersection of interval (1, 4.3) with the interval (2, 6) is equal to the interval (2, 4.3), // whereas Intersetion of interval (1, 4.3) with interval (−4, 0) is an empty interval. We // shall follow the convention that if (ℓ1 , r1 ) does not intersects (ℓ2 , r2 ), and r1 ≤ ℓ2 , // then the intersection interval is (r1 , r1 ). public Interval intersection(Interval I){ if(I.right