Package edu.princeton.cs.algs4
Class Interval1D
- Object
-
- edu.princeton.cs.algs4.Interval1D
-
public class Interval1D extends Object
TheInterval1Dclass represents a one-dimensional interval. The interval is closed—it contains both endpoints. Intervals are immutable: their values cannot be changed after they are created. The classInterval1Dincludes methods for checking whether an interval contains a point and determining whether two intervals intersect.For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Field Summary
Fields Modifier and Type Field Description static Comparator<Interval1D>LENGTH_ORDERCompares two intervals by length.static Comparator<Interval1D>MAX_ENDPOINT_ORDERCompares two intervals by max endpoint.static Comparator<Interval1D>MIN_ENDPOINT_ORDERCompares two intervals by min endpoint.
-
Constructor Summary
Constructors Constructor Description Interval1D(double min, double max)Initializes a closed interval [min, max].
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontains(double x)Returns true if this interval contains the specified value.booleancontains(Interval1D that)Returns true if this interval contains the specified interval.booleanequals(Object other)Compares this transaction to the specified object.inthashCode()Returns an integer hash code for this interval.booleanintersects(Interval1D that)Returns true if this interval intersects the specified interval.doublelength()Returns the length of this interval.static voidmain(String[] args)Unit tests theInterval1Ddata type.doublemax()Returns the max endpoint of this interval.doublemin()Returns the min endpoint of this interval.StringtoString()Returns a string representation of this interval.
-
-
-
Field Detail
-
MIN_ENDPOINT_ORDER
public static final Comparator<Interval1D> MIN_ENDPOINT_ORDER
Compares two intervals by min endpoint.
-
MAX_ENDPOINT_ORDER
public static final Comparator<Interval1D> MAX_ENDPOINT_ORDER
Compares two intervals by max endpoint.
-
LENGTH_ORDER
public static final Comparator<Interval1D> LENGTH_ORDER
Compares two intervals by length.
-
-
Constructor Detail
-
Interval1D
public Interval1D(double min, double max)Initializes a closed interval [min, max].- Parameters:
min- the smaller endpointmax- the larger endpoint- Throws:
IllegalArgumentException- if the min endpoint is greater than the max endpointIllegalArgumentException- if eitherminormaxisDouble.NaN,Double.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITY
-
-
Method Detail
-
min
public double min()
Returns the min endpoint of this interval.- Returns:
- the min endpoint of this interval
-
max
public double max()
Returns the max endpoint of this interval.- Returns:
- the max endpoint of this interval
-
intersects
public boolean intersects(Interval1D that)
Returns true if this interval intersects the specified interval.- Parameters:
that- the other interval- Returns:
trueif this interval intersects the argument interval;falseotherwise
-
contains
public boolean contains(Interval1D that)
Returns true if this interval contains the specified interval.- Parameters:
that- the other interval- Returns:
trueif this interval contains the argument interval;falseotherwise
-
contains
public boolean contains(double x)
Returns true if this interval contains the specified value.- Parameters:
x- the value- Returns:
trueif this interval contains the valuex;falseotherwise
-
length
public double length()
Returns the length of this interval.- Returns:
- the length of this interval (max - min)
-
toString
public String toString()
Returns a string representation of this interval.
-
equals
public boolean equals(Object other)
Compares this transaction to the specified object.
-
hashCode
public int hashCode()
Returns an integer hash code for this interval.
-
main
public static void main(String[] args)
Unit tests theInterval1Ddata type.- Parameters:
args- the command-line arguments
-
-