A unit converter is a program that will convert to and from different units, such as inches and miles. You can input "1 mile" and it will output "63360 inches". This is a tutorial for making a program like this in a couple different programming languages. I'm starting with just Java and PHP, but I may add more later.
The easiest way to do this is in 2 steps. First, convert the entered number into a base unit. Second, convert the base unit into the desired unit. So, to convert inches to miles, you would convert inches into feet and then feet into miles. This might sound more complicated than converting directly from inches to miles, but it is actually much easier to program.
To make this work, we first need to define a bunch of constants for the first step. Here are a few we might need for this example:
- INCHES_IN_FOOT = 12
- CENTIMETERS_IN_FOOT = 30.48
- MILES_IN_FOOT = 0.000189
We could also define constants for the second step, but it is not really needed.
- FEET_IN_INCH = (INCHES_IN_FOOT)-1 = 1/12
- FEET_IN_CENTIMETER = (CENTIMETERS_IN_FOOT)-1 = 1/30.48
- FEET_IN_MILE = (MILES_IN_FOOT)-1 = 1/0.000189
Now that we have constants, the first step is fairly simple. I'll use a generic switch case statement to demonstrate.
switch (starting_unit) case "inch": return (starting_value / INCHES_IN_FOOT); case "centimeter": return (starting_value / CENTIMETERS_IN_FOOT); case "mile": return (starting_value / MILES_IN_FOOT); case "foot": return (starting_value);
The second step is almost identical to the first. We just multiply by the constant instead of divide.
switch (ending_unit) case "inch": return (base_value * INCHES_IN_FOOT); case "centimeter": return (base_value * CENTIMETERS_IN_FOOT); case "mile": return (base_value * MILES_IN_FOOT); case "foot": return (base_value);
Now for the complete program in the different languages. I assumed the variables "starting_value", "starting_unit", and "ending_unit" are already filled with data. I'll leave that part up to you.
Java
There is no way to switch on a String in Java, so I'm using if/else statements instead. I embedded these statements in a helper method to make the code more readable.
class UnitConverter { //Declare constants public static final double INCHES_IN_FOOT = 12; public static final double CENTIMETERS_IN_FOOT = 30.48; public static final double MILES_IN_FOOT = 0.000189; public static void main(String args[]) { //Initialize variables double starting_value, base_value, end_value; String starting_unit, ending_unit; //get value converted to base unit base_value = starting_value / getConstant(starting_unit); //get value converted to ending unit end_value = base_value * getConstant(ending_unit); System.out.println(end_value); } private double getConstant(String unit) { if(unit=="inch") return INCHES_IN_FOOT; else if(unit=="centimeter") return CENTIMETERS_IN_FOOT; else if(unit=="mile") return MILES_IN_FOOT; else return 1; } }
PHP
PHP does allow Stings in a switch statement, but I still embedded it in a helper function for readability.
//define constants define("INCHES_IN_FOOT",12); define("CENTIMETERS_IN_FOOT",30.48); define("MILES_IN_FOOT",0.000189); //convert to base unit $base_value = $start_value / getConstant($starting_unit); //convert to end unit $end_value = $base_value * getConstant($ending_unit); //print end value echo $end_value; function getConstant($unit) { switch($unit) { case "inch": return INCHES_IN_FOOT; case "centimeter": return CENTIMETERS_IN_FOOT; case "mile": return MILES_IN_FOOT; default: return 1; } }