Advertisement
advictoriam

Untitled

Jan 8th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. public class Interval
  2. {
  3.    private double lower;
  4.    private double upper;
  5.  
  6.    /**
  7.       Constructs an open interval.
  8.       @param aLowerBound the lower bound
  9.       @param anUpperBound the upper bound
  10.    */
  11.    public Interval(double aLowerBound, double anUpperBound)
  12.    {
  13.       lower = aLowerBound;
  14.       upper = anUpperBound;
  15.    }
  16.  
  17.    /**
  18.       Checks whether a value is contained in the interval.
  19.       @param x a value
  20.       @return true if x is contained in the interval
  21.    */
  22.    public boolean contains(double x)
  23.    {
  24.       return lower < x && x < upper;
  25.    }
  26.  
  27.    /**
  28.       Gets the lower bound of this interval.
  29.       @return the lower bound
  30.    */
  31.    public double getLower()
  32.    {
  33.       return lower;
  34.    }
  35.  
  36.    /**
  37.       Gets the lower bound of this interval.
  38.       @return the lower bound
  39.    */
  40.    public double getUpper()
  41.    {
  42.       return upper;
  43.    }
  44.  
  45.    /**
  46.       Checks whether this interval overlaps with another.
  47.       @param other another interval
  48.       @return true if this interval and other overlap, i.e.
  49.       have a non-empty intersection
  50.    */
  51.    public boolean overlaps(Interval other)
  52.    {
  53.       if(other.getLower() < this.getUpper() && other.getUpper() > this.getLower()){return true;}
  54.       return false;
  55.    }
  56.  
  57.    // this method is used to check your work
  58.  
  59.    public static boolean check(double lower1, double upper1,
  60.       double lower2, double upper2)
  61.    {
  62.       Interval first = new Interval(lower1, upper1);
  63.       Interval second = new Interval(lower2, upper2);
  64.       return first.overlaps(second);
  65.    }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement