Introduction
Unit conversion is a common task in many fields, including science, engineering, and everyday life. Whether you’re converting units of length, weight, or volume, having a reliable tool to convert between different units is essential. In this program, we will create a simple unit converter application in Java that allows users to convert between various units of length, weight, and volume.
This Java program will provide a simple command-line interface where the user can input the type of conversion (length, weight, or volume) and the values to convert between different units. The program will handle multiple units of measurement and output the corresponding value in the desired unit.
Objective
The objective of this program is to:
- Provide a simple way to convert between different units of measurement for length, weight, and volume.
- Allow the user to select the type of conversion (length, weight, or volume).
- Implement multiple units for each type of measurement (e.g., meters, kilometers, inches for length).
Code
import java.util.Scanner; public class UnitConverter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Unit Converter"); System.out.println("Choose the type of conversion:"); System.out.println("1. Length"); System.out.println("2. Weight"); System.out.println("3. Volume"); System.out.print("Enter your choice (1/2/3): "); int choice = scanner.nextInt(); switch (choice) { case 1: convertLength(scanner); break; case 2: convertWeight(scanner); break; case 3: convertVolume(scanner); break; default: System.out.println("Invalid choice. Exiting."); } scanner.close(); } public static void convertLength(Scanner scanner) { System.out.println("Length Conversion"); System.out.print("Enter value to convert: "); double value = scanner.nextDouble(); System.out.println("Select the unit to convert from:"); System.out.println("1. Meters"); System.out.println("2. Kilometers"); System.out.println("3. Miles"); System.out.print("Enter your choice (1/2/3): "); int fromUnit = scanner.nextInt(); System.out.println("Select the unit to convert to:"); System.out.println("1. Meters"); System.out.println("2. Kilometers"); System.out.println("3. Miles"); System.out.print("Enter your choice (1/2/3): "); int toUnit = scanner.nextInt(); double result = 0; if (fromUnit == 1 && toUnit == 2) { result = value / 1000; } else if (fromUnit == 1 && toUnit == 3) { result = value * 0.000621371; } else if (fromUnit == 2 && toUnit == 1) { result = value * 1000; } else if (fromUnit == 2 && toUnit == 3) { result = value * 0.621371; } else if (fromUnit == 3 && toUnit == 1) { result = value / 0.000621371; } else if (fromUnit == 3 && toUnit == 2) { result = value / 0.621371; } else { result = value; } System.out.println("Converted value: " + result); } public static void convertWeight(Scanner scanner) { System.out.println("Weight Conversion"); System.out.print("Enter value to convert: "); double value = scanner.nextDouble(); System.out.println("Select the unit to convert from:"); System.out.println("1. Kilograms"); System.out.println("2. Grams"); System.out.println("3. Pounds"); System.out.print("Enter your choice (1/2/3): "); int fromUnit = scanner.nextInt(); System.out.println("Select the unit to convert to:"); System.out.println("1. Kilograms"); System.out.println("2. Grams"); System.out.println("3. Pounds"); System.out.print("Enter your choice (1/2/3): "); int toUnit = scanner.nextInt(); double result = 0; if (fromUnit == 1 && toUnit == 2) { result = value * 1000; } else if (fromUnit == 1 && toUnit == 3) { result = value * 2.20462; } else if (fromUnit == 2 && toUnit == 1) { result = value / 1000; } else if (fromUnit == 2 && toUnit == 3) { result = value * 0.00220462; } else if (fromUnit == 3 && toUnit == 1) { result = value / 2.20462; } else if (fromUnit == 3 && toUnit == 2) { result = value * 453.592; } else { result = value; } System.out.println("Converted value: " + result); } public static void convertVolume(Scanner scanner) { System.out.println("Volume Conversion"); System.out.print("Enter value to convert: "); double value = scanner.nextDouble(); System.out.println("Select the unit to convert from:"); System.out.println("1. Liters"); System.out.println("2. Milliliters"); System.out.println("3. Gallons"); System.out.print("Enter your choice (1/2/3): "); int fromUnit = scanner.nextInt(); System.out.println("Select the unit to convert to:"); System.out.println("1. Liters"); System.out.println("2. Milliliters"); System.out.println("3. Gallons"); System.out.print("Enter your choice (1/2/3): "); int toUnit = scanner.nextInt(); double result = 0; if (fromUnit == 1 && toUnit == 2) { result = value * 1000; } else if (fromUnit == 1 && toUnit == 3) { result = value * 0.264172; } else if (fromUnit == 2 && toUnit == 1) { result = value / 1000; } else if (fromUnit == 2 && toUnit == 3) { result = value * 0.000264172; } else if (fromUnit == 3 && toUnit == 1) { result = value / 0.264172; } else if (fromUnit == 3 && toUnit == 2) { result = value / 0.000264172; } else { result = value; } System.out.println("Converted value: " + result); } }
Program Explanation
This Java program provides a simple console-based unit converter that allows users to convert between different units of length, weight, and volume. Here’s a breakdown of the program’s components:
- Scanner: The program uses a
Scanner
object to capture user input from the command line. - Menu: The program displays a menu where the user selects the type of conversion (length, weight, or volume). Based on the user’s choice, the appropriate conversion function is called.
- Conversion Methods: For each type of conversion (length, weight, and volume), there is a corresponding method. These methods prompt the user to enter the value and select the units for conversion, then perform the conversion using simple arithmetic.
- Unit Conversion: Each conversion method uses conditional statements to calculate the equivalent value in the desired unit based on the input values and units selected by the user.
How to Run the Program
To run the program, follow these steps:
- Save the code in a file named
UnitConverter.java
. - Open a terminal or command prompt and navigate to the directory where the file is saved.
- Compile the program with the following command:
javac UnitConverter.java
- Run the program:
java UnitConverter
- Follow the on-screen instructions to choose the type of conversion and input values to convert.