How to Create a Java Unit Converter
by eabrosius in Circuits > Tools
93969 Views, 11 Favorites, 0 Comments
How to Create a Java Unit Converter
In this tutorial, you will learn how to create a unit-converting Java program. This can be useful when you need a quick method of converting from one unit to another, especially when you do not have internet access.
This tutorial will take you about 10-15 minutes to complete.
Before you start the tutorial, you will want to make sure you download Eclipse, which is an Integrated development environment (IDE) where you will create and run your finished code. You also may find it difficult to fully understand what is happening in the code if you do not have a basic understanding of the Java programming language. You will be able to create and use the program nonetheless.
This tutorial will take you about 10-15 minutes to complete.
Before you start the tutorial, you will want to make sure you download Eclipse, which is an Integrated development environment (IDE) where you will create and run your finished code. You also may find it difficult to fully understand what is happening in the code if you do not have a basic understanding of the Java programming language. You will be able to create and use the program nonetheless.
Getting Set Up
Create a Skeleton for LengthConverter
Note:
It is usually a good idea to make comments in your code. Make notes above methods that explain what the methods do, explain the parameters being passed to methods, explain the return values for methods, etc. This will help you to understand what you're trying to accomplish, and it will help people reading your code in the future to understand what you were trying to accomplish.
It is usually a good idea to make comments in your code. Make notes above methods that explain what the methods do, explain the parameters being passed to methods, explain the return values for methods, etc. This will help you to understand what you're trying to accomplish, and it will help people reading your code in the future to understand what you were trying to accomplish.
Create Any Instance Variables
Arbitrary Conversion Factor
Inside of your LengthConverter class, create an instance variable that will serve as the factor between meters and any other length measurement. Let's name this variable factor so that it may be referred to later in this tutorial. This variable should be designated “private” for code security and “double” due to the fact that it will likely be a floating point number.
Other Instance Variables
The only other instance variables you may want to implement in this class would be ones that hold the actual numeric values of the factors between meters and another length measurement. For example, the multiplying factor between inches and meters is 0.0254, so it may be helpful to create a variable for this value. All of these variables should be designated "final private double" which will prevent the value from ever being modified.
Example:
final private double INCH_TO_METER = 0.0254
Note:
You must look up the conversion factors between meters and any other units of length you wish to account for.
Inside of your LengthConverter class, create an instance variable that will serve as the factor between meters and any other length measurement. Let's name this variable factor so that it may be referred to later in this tutorial. This variable should be designated “private” for code security and “double” due to the fact that it will likely be a floating point number.
Other Instance Variables
The only other instance variables you may want to implement in this class would be ones that hold the actual numeric values of the factors between meters and another length measurement. For example, the multiplying factor between inches and meters is 0.0254, so it may be helpful to create a variable for this value. All of these variables should be designated "final private double" which will prevent the value from ever being modified.
Example:
final private double INCH_TO_METER = 0.0254
Note:
You must look up the conversion factors between meters and any other units of length you wish to account for.
Implement Constructor
The purpose of the constructor is to create an instance of a LengthConverter object. Any instance of this type of object will have a String object as a parameter that represents a unit of length. For example, if I were to construct a LengthConverter object that converts between inches and the “middle” unit, meters, I would write,
LengthConverter inchesToMeters = new LengthConverter (“in”);
The String “in” is the parameter for the constructor, and it signals that this object will work with the inch-to-meter conversion factor.
The constructor must check this parameter for common units of length (in, ft, mi, cm, yd, etc.), and it must set the value of the instance variable factor accordingly. In the case where inches is the parameter, factor would be set to 0.0254 because one inch is equivalent to 0.0254 meters.
LengthConverter inchesToMeters = new LengthConverter (“in”);
The String “in” is the parameter for the constructor, and it signals that this object will work with the inch-to-meter conversion factor.
The constructor must check this parameter for common units of length (in, ft, mi, cm, yd, etc.), and it must set the value of the instance variable factor accordingly. In the case where inches is the parameter, factor would be set to 0.0254 because one inch is equivalent to 0.0254 meters.
Implement ToMeters()
This method simply takes a measurement (passed as a parameter) in the user-specified input units, and returns its equivalent value in meters. Multiply the parameter by the instance variable, factor, and return the result.
Implement FromMeters()
This method takes a measurement in meters (passed as a parameter) and returns its equivalent value in the user-specified output units. Divide the parameter by the instance variable, factor, and return the result.
Create a Main() Method
Create another Class within the length package in the same way that you created LengthConverter in Step 1. Name this class ConversionCalculator. This Class will contain the program’s main() method, which is the heart of any Java program. When you click “Run,” the main() method is called first, and any other methods are called within the main() method.
The most commonly used syntax for a main() method is,
public static void main(String[] args) {
}
The most commonly used syntax for a main() method is,
public static void main(String[] args) {
}
Scanner and User Inputs
Create a Scanner
In order to interact with the user, a program must utilize a Scanner object. A Scanner object can read what a user types into the command line. This Scanner will read the numeric value the user wishes to convert, the units the user wishes to convert from, and the units the user wishes to convert to.
To create a Scanner to read from the command line, type the following:
Scanner in = new Scanner (System.in);
Note: “in” is the name of the Scanner in this code.
Note: Copy and paste the following line of code outside of the class declaration in order for Eclipse to recognize a Scanner object.
import java.util.Scanner;
Obtain “from” and “to” Units
Print out a message that asks the user what units to convert from. Call the Scanner’s nextLine() method to obtain this String.
Do the same for whatever units the user wants to convert to.
Note: It may be helpful to inform the user of legal values to enter. For instance, if your code is only built to handle "in" and not the full word "inches," then you should either edit your code to account for this or let the user know that "in" is the only legal input value.
In order to interact with the user, a program must utilize a Scanner object. A Scanner object can read what a user types into the command line. This Scanner will read the numeric value the user wishes to convert, the units the user wishes to convert from, and the units the user wishes to convert to.
To create a Scanner to read from the command line, type the following:
Scanner in = new Scanner (System.in);
Note: “in” is the name of the Scanner in this code.
Note: Copy and paste the following line of code outside of the class declaration in order for Eclipse to recognize a Scanner object.
import java.util.Scanner;
Obtain “from” and “to” Units
Print out a message that asks the user what units to convert from. Call the Scanner’s nextLine() method to obtain this String.
Do the same for whatever units the user wants to convert to.
Note: It may be helpful to inform the user of legal values to enter. For instance, if your code is only built to handle "in" and not the full word "inches," then you should either edit your code to account for this or let the user know that "in" is the only legal input value.
Construct Two LengthConverter Objects
At this point, you must construct the two LengthConverter objects necessary to perform the unit conversion. The first object will take care of the conversion between the “from” units and meters. The second object will take care of the conversion between meters and the “to” units. Let’s name the first object “from” and the second object “to” so that they may easily be referred to in later steps.
Obtain the Value to Be Converted
Print out a message that prompts the user to type the numeric value that is to be converted. Use the Scanner’s nextDouble() method to grab this value, and then assign it to a double variable. Let’s call this variable “val” so that it may be referred to in later steps.
"From" to Meters
Call the toMeters() method on the first LengthConverter object, from, and use val as the parameter that the toMeters() method requires. Assign the returned value (which is now in meters) to a double variable. Let’s call this variable meters so that it may be referred to in later steps.
Meters to "To"
Call the fromMeters() method on the second LengthConverter object, to, and use meters as the parameter that the fromMeters() method requires. Assign the returned value (which is now in the desired output units) to a double variable. Let’s call this variable converted so that it may be referred to in later steps.