CS 100: Lecture L10

February 25

| Back to Lecture Index |


Boolean-valued methods via the Class Interval:

public class Interval
{
   // An instance of this class is a particular interval [L,R].
   // That is, the set of all real numbers x that satisfy L<=x<=R.
   
   private double L;   // The left endpoint.
   private double R;   // The right endpoint. (L<=R)
   
   
   // Constructor that creates an interval defined by all points
   // between and including a and b.
   public Interval(double a, double b)
   {
      if (a<=b)
      {
         L = a;
         R = b;
      }
      else
      {
         L = b;
         R = a;
      }
   }
   
   // Yields true if this interval contains x.
   public boolean contains(double x)
   {
      boolean B1 = L<=x;
      boolean B2 = x<=R;
      return B1 && B2;
   }
   
   // Yields true if this interval and q have at least one point in common.
   public boolean intersect(Interval q)
   {
      boolean B1 = R < q.L;
      boolean B2 = L > q.R;
      return !(B1 || B2);
   }
   
   // Yields true if every point in q is in this interval.
   public boolean contains(Interval q)
   {
      boolean B1 = L<=q.L;
      boolean B2 = R>=q.R;
      return B1 && B2;
   }
      
   // Yields true if every point in q is in this interval or if every point in
   // this interval is in q.
   public boolean nestable(Interval q)
   {
      boolean B1 = this.contains(q);
      boolean B2 = q.contains(this);
      return B1 || B2;
   } 
   
   // Yields an interval whose endpoints are randomly selected from the interval q
   public static Interval random(Interval q)
   {
      double d = q.R-q.L;
      double a = q.L + Math.random()*d;
      double b = q.L + Math.random()*d;
      Interval I = new Interval(a,b);
      return I;
   }
    
   // Pretty prints this interval
   public void println()
   {
      Format.print(System.out,"[%4.2f,",L);
      Format.println(System.out,"%4.2f]",R);
   }
     
}


Examples that show how to use the methods in the class Interval:

// 

import java.io.*;

public class ShowInterval
{	
	public static void main(String args[])
	{
           TokenReader in = new TokenReader(System.in);
	   
	   Interval I1 = new Interval(1,5);
	   System.out.print("I1: ");
	   I1.println();
	   Interval I2 = new Interval(4,7);
	   System.out.print("I2: ");
	   I2.println();
	   Interval I3 = new Interval(2,3);
	   System.out.print("I3: ");
	   I3.println();
	   System.out.println(" ");
	   
	   // Illustrate the method contains with a double actual parameter.
	   if (I1.contains(2.0))
	      {System.out.println("I1 contains " + 2.0 );}
	   else
	      {System.out.println("I1 does not contain " + 2.0 );}
	   if (I2.contains(2))
	      {System.out.println("I2 contains " + 2.0 );}
	   else
	      {System.out.println("I2 does not contain " + 2.0 );}
	      
	   // Illustrate the method intersect.
	   if (I1.intersect(I2))
	      {System.out.println("I1 intersects I2");}
	   else
	      {System.out.println("I1 does not intersect I2");}
	   if (I2.intersect(I3))
	      {System.out.println("I2 intersects I3");}
	   else
	      {System.out.println("I2 does not intersect I3");}
	      
	   // Illustrate the method contains with an Interval actual parameter..
	   if (I1.contains(I2))
	      {System.out.println("I1 contains I2");}
	   else
	      {System.out.println("I1 does not contain I2");}    
	   if (I1.contains(I3))
	      {System.out.println("I1 contains I3");}
	   else
	      {System.out.println("I1 does not contain I3");}    
	      
	   
	      
	   // Illustrate the method nestable.
	   if (I2.nestable(I1))
	      {System.out.println("I1 and I2 are nestable");}
	   else
	      {System.out.println("I1 and I2 are not nestable");}    
	   if (I1.nestable(I3))
	      {System.out.println("I1 and I3 are nestable");}
	   else
	      {System.out.println("I1 and I3 are not nestable");} 
	   if (I3.nestable(I1))
	      {System.out.println("I3 and I1 are nestable");}
	   else
	      {System.out.println("I3 and I1 are not nestable");}   
	      
	   // Illustrate the method random by estimating the probability that
	   // two randomly generated intervals chosen from [0,10] intersect.
	   Interval J1,J2;
	   Interval I = new Interval(0,10);
	   int count = 0;
	   for(int k=1;k<=1000;k++)
	   {
	      J1 = Interval.random(I);
	      J2 = Interval.random(I);
	      if (J1.nestable(J2))
	         {count++;}
	   }
	   System.out.println("\nProbability estimate = " + (double)count/1000);     
	   in.waitUntilEnter();
	}

}