public class Weight extends Object
Modifier and Type | Field and Description |
---|---|
static String |
KG
The abbreviation of kilogram.
|
static double |
KG_PER_LB
The number of kilograms in one pound.
|
static String |
LB
The abbreviation of pounds.
|
Constructor and Description |
---|
Weight()
Initializes the weight to 0 kg.
|
Weight(double wt,
String units)
Initializes the weight to the given numeric value having the given
units.
|
Weight(Weight other)
Initializes the weight by copying the numeric value and units of
another weight.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
Compares two weights for equality.
|
double |
get()
Gets the numeric value of the weight using the current units.
|
String |
getUnits()
Gets the units of the weight.
|
void |
set(double wt)
Sets the numeric value of the weight using the current units.
|
void |
setUnits(String units)
Set the units of the weight.
|
static double |
toKilograms(double lb)
Converts a value of pounds to the equivalent value of kilograms.
|
static double |
toPounds(double kg)
Converts a value of kilograms to the equivalent value of pounds.
|
String |
toString()
Returns a string made up of the numeric value of the weight followed
by a space followed by its units.
|
public static final String KG
public static final String LB
public static final double KG_PER_LB
public Weight()
public Weight(Weight other)
other
- the weight to copypublic Weight(double wt, String units)
wt
- the weightunits
- the units (Weight.KG
or
Weight.LB
)IllegalArgumentException
- if the weight is less than zeroIllegalArgumentException
- if units
is not one of
Weight.KG
or
Weight.LB
Weight.KG
or Weight.LB
public double get()
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();
public final void set(double wt)
w
is 15.0
kilograms in the example below:
Weight w = new Weight(0, Weight.KG); w.set(15.0);
wt
- the numeric value of the weightIllegalArgumentException
- if the weight is less than zeropublic String getUnits()
Weight.KG
if the weight is in
kilograms, or Weight.LB
if the
weight is in pounds.public final void setUnits(String units)
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
units
- the units of the weight (Weight.KG
or Weight.LB
)IllegalArgumentException
- if units
is not one of
Weight.KG
or
Weight.LB
Weight.KG
or Weight.LB
public static double toPounds(double kg)
LB = KG / Weight.KG_PER_LB
where LB is the weight in pounds and KG is the weight in kilograms.
kg
- a weight in kilogramspublic static double toKilograms(double lb)
KG = LB × Weight.KG_PER_LB
where LB is the weight in pounds and KG is the weight in kilograms.
lb
- a weight in poundspublic String toString()
12.5 kg 73.01234 lb
are returned for weights of 12.5
kilograms and
73.01234
pounds, respectively.
public boolean equals(Object obj)
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.