Class Length

  • All Implemented Interfaces:
    java.lang.Comparable<Length>

    public class Length
    extends java.lang.Object
    implements java.lang.Comparable<Length>
    A length measurement that can be represented in centimetres, inches, or points.
    Author:
    Chris Jennings
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int CM
      Centimetres
      static int IN
      Inches
      static int PT
      Points
    • Constructor Summary

      Constructors 
      Constructor Description
      Length()
      Creates a new length representing a distance of zero.
      Length​(double measurement, int unit)
      Creates a new length equivalent to the distance specified by the measurement and unit.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(Length rhs)
      Compares this length with the specified length.
      static double convert​(double measurement, int sourceUnit, int destUnit)
      Returns the a measurement in terms of a different unit.
      boolean equals​(java.lang.Object obj)
      Returns true if and only if the compared object is a length of equal size.
      double get​(int unit)
      Returns the value of this length measurement in the requested unit.
      static int getDefaultUnit()
      Returns the default unit to use when displaying lengths to the user.
      double getPoints()
      Returns this length in points.
      int hashCode()  
      void set​(double measurement, int unit)
      Sets this length to the distance specified by the measurement and unit.
      static void setDefaultUnit​(int unit)
      Sets the default unit to use when displaying lengths to the user.
      void setPoints​(double points)
      Sets the value of this length measurement in points.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Length

        public Length()
        Creates a new length representing a distance of zero.
      • Length

        public Length​(double measurement,
                      int unit)
        Creates a new length equivalent to the distance specified by the measurement and unit.
        Parameters:
        measurement - the magnitude of the distance
        unit - the unit in which the measurement is expressed
    • Method Detail

      • set

        public void set​(double measurement,
                        int unit)
        Sets this length to the distance specified by the measurement and unit.
        Parameters:
        measurement - the magnitude of the distance
        unit - the unit in which the measurement is expressed
      • get

        public double get​(int unit)
        Returns the value of this length measurement in the requested unit.
        Parameters:
        unit - the desired unit, such as CM, IN, or PT
        Returns:
        this object's length measurement expressed in the specified unit
      • setPoints

        public void setPoints​(double points)
        Sets the value of this length measurement in points.
        Parameters:
        points - the length to set, in points
      • getPoints

        public double getPoints()
        Returns this length in points.
        Returns:
        the distance represented by this length, in points
      • compareTo

        public int compareTo​(Length rhs)
        Compares this length with the specified length. Returns a negative integer, zero, or a positive integer as this length is less than, equal to, or greater than the specified length.
        Specified by:
        compareTo in interface java.lang.Comparable<Length>
        Parameters:
        rhs - the length to be compared with this length
        Returns:
        an integer whose sign is consistent with the sign of this.get(PT) - rhs.get(PT)
      • equals

        public boolean equals​(java.lang.Object obj)
        Returns true if and only if the compared object is a length of equal size.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to compare this to
        Returns:
        true if obj is a Length that represents the same distance
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • convert

        public static double convert​(double measurement,
                                     int sourceUnit,
                                     int destUnit)
        Returns the a measurement in terms of a different unit.
        Parameters:
        measurement - the measurement to convert
        sourceUnit - the unit used for the source measurement
        destUnit - the unit used for the destination measurement
        Returns:
        the measurement in the source unit converted to the destination unit
      • getDefaultUnit

        public static int getDefaultUnit()
        Returns the default unit to use when displaying lengths to the user. If no default unit is set, then the default unit will be CM unless the default locale has a country value of "US", in which case the default unit will be IN.
        Returns:
        the default unit for presenting length measurements to the user
      • setDefaultUnit

        public static void setDefaultUnit​(int unit)
        Sets the default unit to use when displaying lengths to the user.
        Parameters:
        unit - the unit to set
        Throws:
        java.lang.IllegalArgumentException - if the unit is not one of the legal values