How to Convert Feet to Meters in Java
Computer programs often need to deal with metric and imperial units, so it's important for programmers to know how to convert between them. Once your program can handle converting feet to meters, you won't have trouble adding support for other units to your program as well.
Things You'll Need
- Text editor
- Java compiler
- Program that needs unit conversion capability
Instructions
-
-
1
Open your text editor and create a file named Units.java.
-
2
Add a class named Units. It should look like this:
public class Units
{
} -
-
3
Add a static function named feetToMeters to your class. It should take a double as an argument and return a double as its result, as follows:
public static double feetToMeters(double feet)
{
} -
4
Add code inside the function to do the conversion and return the result:
return feet / 3.28084;
-
5
Add your new .java file to your program. By now, it should look like this:
public class Units
{
public static double feetToMeters(double feet)
{
return feet / 3.28084;
}
} -
6
Use your new class whenever you need to convert from one unit to another. You can convert feet to meters by writing Units.feetToMeters(someNumberOfFeet), replacing "someNumberOfFeet" with the variable you need to convert.
-
1
Tips & Warnings
Whenever you need different conversion functions, add them to the same units class. This will help keep your program organized and easy to maintain.