October 30th, 2012, 01:56 PM
-
Imperial weight conversions to kilograms.
Here's another homework-type question, converting UK pounds to the various imperial weights and finally to kilograms. Again, this probably isn't 100% perfect, but it does the job.
Java Code:
/**
* This will convert imperial measurements relating to weight
* to the metric equivalent. Please see
* the methods at the bottom of the code to see how the
* conversion is done. Note: Some of the calculations might
* not be perfect when checked against an online calculator
* but no online solution posted in a forum should be
* considered perfect, so it's up to you to refine the code
* if you think that is required.
*
* @author: Shaun B
* @version: 2012-10-29
**/
import java.lang.Math;
import java.util.Scanner;
public class exerciseTwo
{
// This is a constant available at a class level
private static final float kilosPerPound = (float) 0.45359;
// This is a class level variable (privately declared) which will be
// used within the main method and any other methods that require it:
private static int pounds = 0;
// This will read in our keyboard inputs via the Scanner utility:
private static Scanner keyboardInput = new Scanner(System.in);
// This will tell the main routine to continue running:
private static boolean run = true;
// Here is the main method, ie, where it runs from:
public static void main(String[] args)
{
System.out.println("This will demonstrate converting imperial weights (Pounds");
System.out.println("and ounces) to metric weights. Please enter the number of");
System.out.println("the number of pounds you wish to convert as a whole number");
System.out.println("or enter 0 (zero) to exit this demonstration.");
while(run)
{
System.out.print("C:\\>");
try
{
pounds = keyboardInput.nextInt();
}
catch(Exception e)
{
System.err.println("Illegal keyboard entry in main class");
run = false;
System.exit(0);
}
if(pounds == 0)
{
System.out.println("Goodbye!");
run = false;
break;
}
System.out.println("Here is the conversion:");
System.out.format("%d is %f ton(s)\n",pounds,tons(pounds));
System.out.format("%d is %f hundredweight\n",pounds,hundredWeight(pounds));
System.out.format("%d is %f quarter\n",pounds,quarter(pounds));
System.out.format("%d is %f stone\n",pounds,stone(pounds));
System.out.format("%d is %d ounces\n",pounds,ounces(pounds));
System.out.format("%d is %d drachm\n",pounds,drachm(pounds));
System.out.format("%d is %d grains\n",pounds,grain(pounds));
System.out.format("%d is %f kilograms\n",pounds,kilograms(pounds));
}
System.exit(0);
}
public static float tons(int x)
{
return (float)x/2240;
}
public static float hundredWeight(int x)
{
return (float)x/112;
}
public static float quarter(int x)
{
return (float)x/28;
}
public static float stone(int x)
{
return (float)x/14;
}
public static int ounces(int x)
{
return x*16;
}
public static int drachm(int x)
{
// A drachm is 1/256 pounds, so we can call the ounces
// method above and multiply that by 16, as 16 is the
// square root of 256:
return ounces(x)*16;
}
public static long grain(int x)
{
return (long)x*7000;
}
public static float kilograms(int x)
{
return (float)x*kilosPerPound;
}
}
Feel free to ask any questions,
Shaun.