Source of Line.java


  2: /**
  3:  * A class to hold information about a line -- used only for a demo of
  4:  * sorting objects by a double value.
  5:  *
  6:  * @author Mark Young (A00000000)
  7:  */
  8: public class Line implements Comparable<Line> {

 10:     private double length;

 12:     /** construct a line with the given length */
 13:     public Line(double len) {
 14:         if (len < 0) {
 15:             throw new IllegalArgumentException("Illegal length: " + len);
 16:         }
 17:         length = len;
 18:     }

 20:     /** sort lines from shortest to longest */
 21:     public int compareTo(Line other) {
 22:         double result = this.length - other.length;
 23:         double sign = Math.signum(result);
 24:         return (int) sign;
 25:     
 26:         // OR we could have just had:
 27:         // return (int)(Math.signum(this.length - other.length));
 28:     }

 30:     /** convert Line to String */
 31:     @Override
 32:     public String toString() {
 33:         return "Line (" + length + " cm)";
 34:     }

 36: }