Java 11 Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

Those who prefer the metric system of units can now take advantage of the new method:

public static void main(String... arg) {
int horsePower = 246;
int payload = Truck.convertKgToPounds(1500);
int vehicleWeight = Truck.convertKgToPounds(1800);
Truck truck = FactoryVehicle.
buildTruck(payload, vehicleWeight, horsePower);
System.out.println("Payload in pounds: " +
truck.getPayloadPounds());
int kg = truck.getPayloadKg();
System.out.println("Payload converted to kg: " + kg);
System.out.println("Payload converted back to pounds: " +
Truck.convertKgToPounds(kg));
}

The results will be as follows:

The value of 1,502 is close to the original 1,500, while 3,308 is close to 3,312. The difference is caused by the error of an approximation during the conversion.