package test2; /** * A class that represents weights in kilograms or pounds. Every * weight has a numeric value and a string representing the units * of the weight. * */ public class Weight { /** * The abbreviation of kilogram. */ public static final String KG = "kg"; /** * The abbreviation of pounds. */ public static final String LB = "lb"; /** * The number of kilograms in one pound. */ public static final double KG_PER_LB = 0.45359237; private double kg; private double lb; private String units; /** * Initializes the weight to 0 kg. */ public Weight() { this(0.0, Weight.KG); } /** * Initializes the weight by copying the numeric value and units of * another weight. * * @param other * the weight to copy */ public Weight(Weight other) { this(other.get(), other.getUnits()); } /** * Initializes the weight to the given numeric value having the given * units. * * @param wt * the weight * @param units * the units (Weight.KG or Weight.LB) * @throws IllegalArgumentException * if the weight is less than zero * @throws IllegalArgumentException * if units is not one of * Weight.KG or Weight.LB */ public Weight(double wt, String units) { this.setUnits(units); this.set(wt); } /** * Gets the numeric value of the weight using the current units. For * instance, the value of kg is 1.5 and the * value of lb is 5.2 in the following example: * *
     * Weight w = new Weight(1.5, Weight.KG);
     * double kg = w.get();
     * 
     * Weight u = new Weight(5.2, Weight.LB);
     * double lb = u.get();
     * 
* * @return the numeric value of the weight using the current units */ public double get() { if (this.units.equals(Weight.KG)) { return this.kg; } return this.lb; } /** * Sets the numeric value of the weight using the current units. For * instance, the final numeric value of w is 15.0 * kilograms in the example below: * *
     * Weight w = new Weight(0, Weight.KG);
     * w.set(15.0);
     * 
* * @param wt * the numeric value of the weight * @throws IllegalArgumentException * if the weight is less than zero */ public final void set(double wt) { if (wt < 0.0) { throw new IllegalArgumentException(); } if (this.units.equals(Weight.KG)) { this.kg = wt; this.lb = Weight.toPounds(wt); } else { this.lb = wt; this.kg = Weight.toKilograms(wt); } } /** * Gets the units of the weight. * * @return Weight.KG if the weight is in * kilograms, or Weight.LB if the * weight is in pounds. */ public String getUnits() { return this.units; } /** * Set the units of the weight. This method has the side effect that * the numeric value of the weight changes to reflect the change in * units. For instance, changing the units of 1.0 pound * to kilograms causes the value of kg to be equal to * Weight.KG_PER_LB: * *
     * Weight w = new Weight(1.0, Weight.LB);
     * w.setUnits(Weight.KG);
     * double kg = t.get();                 // kg is equal to Weight.KG_PER_LB
     * String units = t.getUnits();         // units is equal to Weight.KG 
     * 
* * @param units * the units of the weight (Weight.KG or Weight.LB) * @throws IllegalArgumentException * if units is not one of Weight.KG or * Weight.LB * */ public final void setUnits(String units) { if (!units.equals(Weight.KG) && !units.equals(Weight.LB)) { throw new IllegalArgumentException(); } this.units = units; } /** * Converts a value of kilograms to the equivalent value of pounds. * The formula to convert kilograms to pounds is: * *

* LB = KG / Weight.KG_PER_LB * *

* where LB is the weight in pounds and KG is the weight * in kilograms. * * @param kg * a weight in kilograms * @return the equivalent value of the weight in pounds */ public static double toPounds(double kg) { return kg / Weight.KG_PER_LB; } /** * Converts a value of pounds to the equivalent value of kilograms. * The formula to convert pounds to kilograms is: * *

* KG = LB × Weight.KG_PER_LB * *

* where LB is the weight in pounds and KG is the weight * in kilograms. * * @param lb * a weight in pounds * @return the equivalent value of the weight in kilograms */ public static double toKilograms(double lb) { return lb * Weight.KG_PER_LB; } /** * Returns a string made up of the numeric value of the weight followed * by a space followed by its units. For example, the Strings * *

     * 12.5 kg
     * 73.01234 lb
     * 
* *

* are returned for weights of 12.5 kilograms and * 73.01234 pounds, respectively. * * @return a string representation of the weight */ @Override public String toString() { if (units.equals(Weight.KG)) { return this.kg + " " + Weight.KG; } else { return this.lb + " " + Weight.LB; } } /** * Compares two weights for equality. Two weights are considered * equal if they represent the same weight expressed in kilograms. * For instance, the values of eq1 and * eq2 are true in the following example: * *

     * Weight w = new Weight(1.0, Weight.LB);               // 1 lb
     * Weight u = new Weight(Weight.KG_PER_LB, Weight.KG);  // 0.45359237 kg
     * boolean eq1 = w.equals(u);    // true
     * boolean eq2 = u.equals(w);    // true
     * 
* *

* because 1.0 pound is equivalent to * Weight.KG_PER_LB kilograms, and vice versa. * * @param obj an object to compare * @return true if obj is a Weight object that represents the same weight * as this weight when expressed in kilograms */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Weight other = (Weight) obj; if (Double.doubleToLongBits(this.kg) != Double.doubleToLongBits(other.kg)) return false; return true; } }