How to Convert Meter to Foot in Java
It's easy to forget sometimes that the primary reason computers were invented was to perform repetitive mathematical tasks, like converting meters and feet. This example will not only illustrate how to create a Java program to perform that conversion, but will also work using good Object Oriented Design (OOD) principles to create a skeleton for an entire package of conversion utilities. This way, you'll leverage the OOD technology of Java to ensure that you only have to solve a problem like this once.
Things You'll Need
- Java Development Kit
- Java Runtime Environment
- Text editor or Java IDE (Netbeans suggested)
Instructions
-
-
1
Create a package. The first thing you will need to do is create a package. A package is just a hierarchy of folders in your project's source directory. Right now, the package will hold only two classes: an abstract Converter class and your MeterToFoot class, but you'll appreciate the extra five minutes when it comes time to perform a metric conversion in another project. You should give your package a name that will be meaningful to you in the future, but unique. The following is just an example; you can use whatever you like:
rollins.amber.conversion
That means that, in this source directory, there will be a folder rollins, and within that a folder amber, and within that a folder conversion. Once you build this set of folders, you can expand it in the future to build your own collection of useful classes for later projects.
-
2
Create Converter abstract class. Inside your package, create a Converter.java file. This abstract class will define some structure for how your conversion classes will function. And you should always write your Javadoc comments:
/**
* This class works as a skeleton for classes that will perform simple metric conversions.
* @author Amber Rollins
*/
abstract class Converter {
/**
* This represents the ratio between the two metrics being converted.
*/
static double conversionRatio;/**
* Perform a conversion using the conversionRatio.
* @param value The value to be converted.
* @return result
*/
public static double convert(double value) {
return (value * conversionRatio);
}
} -
-
3
Create class MeterToFoot. You actually did most of the work already in your abstract Converter class. From now on, any basic conversion can be performed by simply inheriting from the Converter and initializing the conversionRatio field, like so:
/**
* Convert a value in meters to a value in feet.
*@author Amber Rollins
*/
public class MeterToFoot extends Converter {
// One meter is 3.28083989501 feet. (see references 1)
static double conversionRatio = 3.28083989501
} -
4
Create a main function to test your program. You can place your main function anywhere, but for convenience, place it inside your MeterToFoot class. Add an import for java.io.* and java.util.* to your class, and write the following:
public static void main (String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many meters: ");
double meters = Double.parseDouble(in.readLine());
double feet = MeterToFoot.convert(meters);
System.out.println("Feet: " + feet);
}
-
1