Class Length

  • All Implemented Interfaces:
    Comparable<Length>


    public class Length
    extends Object
    implements Comparable<Length>
    This is a measure of length. A Length object includes a value and a unit.

    A set of common units of length are included in this class.

    • Field Detail

      • KILOMETER

        public static final Length.Unit KILOMETER
        The unit definition of a kilometer.
      • METER

        public static final Length.Unit METER
        The unit definition of a meter.
      • DECIMETER

        public static final Length.Unit DECIMETER
        The unit definition of a decimeter.
      • CENTIMETER

        public static final Length.Unit CENTIMETER
        The unit definition of a centimeter.
      • MILLIMETER

        public static final Length.Unit MILLIMETER
        The unit definition of a millimeter.
      • MICROMETER

        public static final Length.Unit MICROMETER
        The unit definition of a micrometer.
      • INCH

        public static final Length.Unit INCH
        The unit definition of an inch.
      • FOOT

        public static final Length.Unit FOOT
        The unit definition of a foot.
      • MILE

        public static final Length.Unit MILE
        The unit definition of a mile.
      • YARD

        public static final Length.Unit YARD
        The unit definition of a yard.
    • Constructor Detail

      • Length

        public Length(double v,
                      Length.Unit u)
        Create a new Length object.
        Parameters:
        v - the number value of this length.
        u - the units that value is measured in.
      • Length

        public Length(Length l)
        Clones a Length object.
    • Method Detail

      • getUnits

        public static Length.Unit[] getUnits()
        Returns all the available units.
      • clone

        public Object clone()
      • setUnit

        public void setUnit(Length.Unit u2)
        Changes the unit of this length, but the actual distance measured remains the same. (That is, if this obviously previously represented "100 cm", and you called setUnit(Length.METER), then this object now represents "1 m".)
      • getUnitByName

        public static Length.Unit getUnitByName(String name)
        Returns a unit associated with a given name.

        This looks for abbreviations and full spellings of unit names.

        Parameters:
        name - a name for a unit. This is not case sensitive.
        Returns:
        the unit associated with that name, or null if no units are found.
      • equals

        public boolean equals(Object obj)
        This returns true if two Length objects measure the same distance, regardless of their units.
      • getValue

        public double getValue(Length.Unit u)
        Returns the numeric value of this distance in the unit specified.
        Parameters:
        u - the unit to get the value in
        Returns:
        the numeric value of this distance
      • setValue

        public void setValue(double v,
                             Length.Unit u)
        Redefine this Length object.
      • add

        public void add(Length l)
        Add the argument to this Length object. The units of both objects are unchanged.
      • subtract

        public void subtract(Length l)
        Subtracts the argument from this Length object. The units of both objects are unchanged.
      • getUnit

        public Length.Unit getUnit()
        Returns the unit of this Length object.
      • compareTo

        public int compareTo(Length l)
        Compares the distance of this Length object with another.
      • toString

        public String toString()
        Returns the value of this length followed by the unit abbreviation. For example: "3 mi" or "2.0 mm".