package eecs2030.test3;
/**
* A class that represents weights in kilograms or pounds. Every
* weight has a numeric value and a string representing the units
* of the weight.
*
* @author EECS2030 Fall 2016
*
*/
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;
/*
* YOU SHOULD ADD WHATEVER FIELDS YOU REQUIRE BELOW HERE
*/
/**
* Initializes the weight to 0 kg.
*/
public Weight() {
}
/**
* Initializes the weight by copying the numeric value and units of
* another weight.
*
* @param other
* the weight to copy
*/
public Weight(Weight other) {
}
/**
* 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) {
}
/**
* 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() { return 0.0; } /** * 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) { } /** * 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 "";
}
/**
* 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) {
}
/**
* 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 0.0; } /** * 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 0.0; } /** * 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 (this.getUnits().equals(Weight.KG)) {
return this.get() + " " + Weight.KG;
} else {
return this.get() + " " + 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.
*/
@Override
public boolean equals(Object obj) {
// 1. you may use the eclipse generated version of equals and hashCode
// 2. you might need to modify the eclipse generated version of equals to
// satisfy the API stated above
// 3. you may leave or remove the eclipse generated version of hashCode;
// it won't be marked
return true;
}
}