For some reason this conversion really mess with my head. I can do the conversion on paper and in my head but when I try to write it for my homework in Java it really messes me up. The assignment is for user to enter a number in kilograms and to write a program that says, this is how many pounds on one line and this is how many ounces on another. Conversions in general mess me up but I’m not sure if this is correct. Do I need to use a type cast anywhere?
import acm.program.*;
public class KilogramsToPoundsAndOunces extends ConsoleProgram {
public void run(){
println("This programs converts kilograms into pounds and ounces.");
double kilo = readDouble("Enter the kilogram value: ");
double totalOunces = (kilo * POUNDS_PER_KILOGRAM) * OUNCES_PER_POUND;
int totalPounds = totalOunces % OUNCES_PER_POUND;
double leftOverOunces = totalOunces - (totalPounds * OUNCES_PER_POUND);
println(totalPounds + "lbs" + ".");
println(leftOverOunces + "ozs" + ".")
}
private static void POUNDS_PER_KILOGRAM = 2.2;
private static void OUNCES_PER_POUND = 16;
}
You need to define numeric data types for your constants:
Also
totalPoundswould need to be cast to anintto compile:Although this should be:
(See @Kleanthis answer)