w3resource

Java Project: Convert Length, Temperature, and Weight

Java Project - Unit Conversion:

Unit Converter:

Convert between various units (e.g., length, temperature, weight).

A tool that allows users to convert between different units such as length (meters to miles), temperature (Celsius to Fahrenheit), or weight (kilograms to pounds). The user inputs the value and selects the units for conversion.

Input: Value, unit type (e.g., length, weight), and unit to convert to.

Output: Converted value in the target unit.

Example:

  • Input: 10 kilometers to miles
  • Output: 6.21371 miles

Here are two different solutions for the Unit Converter project in Java, focusing on converting between units of length, temperature, and weight:

Solution 1: Using If-Else Statements

Code:

import java.util.Scanner;

public class UnitConverterIfElse {
    public static void main(String[] args) {
        // Create a Scanner object to capture user input
        Scanner scanner = new Scanner(System.in);

        // Input the value to be converted
        System.out.print("Enter the value: ");
        double value = scanner.nextDouble();

        // Input the type of conversion (length, temperature, weight)
        System.out.print("Enter the unit type (length, temperature, weight): ");
        String unitType = scanner.next().toLowerCase();

        // Input the unit to convert from
        System.out.print("Enter the unit to convert from (e.g., kilometers, celsius, kilograms): ");
        String fromUnit = scanner.next().toLowerCase();

        // Input the unit to convert to
        System.out.print("Enter the unit to convert to (e.g., miles, fahrenheit, pounds): ");
        String toUnit = scanner.next().toLowerCase();

        // Variable to store the result
        double convertedValue = 0;

        // Perform the conversion based on unit type using if-else statements
        if (unitType.equals("length")) {
            // Length conversion (e.g., kilometers to miles)
            if (fromUnit.equals("kilometers") && toUnit.equals("miles")) {
                convertedValue = value * 0.621371;
            } else if (fromUnit.equals("miles") && toUnit.equals("kilometers")) {
                convertedValue = value * 1.60934;
            } else {
                System.out.println("Invalid length conversion.");
                return;
            }
        } else if (unitType.equals("temperature")) {
            // Temperature conversion (e.g., Celsius to Fahrenheit)
            if (fromUnit.equals("celsius") && toUnit.equals("fahrenheit")) {
                convertedValue = (value * 9/5) + 32;
            } else if (fromUnit.equals("fahrenheit") && toUnit.equals("celsius")) {
                convertedValue = (value - 32) * 5/9;
            } else {
                System.out.println("Invalid temperature conversion.");
                return;
            }
        } else if (unitType.equals("weight")) {
            // Weight conversion (e.g., kilograms to pounds)
            if (fromUnit.equals("kilograms") && toUnit.equals("pounds")) {
                convertedValue = value * 2.20462;
            } else if (fromUnit.equals("pounds") && toUnit.equals("kilograms")) {
                convertedValue = value * 0.453592;
            } else {
                System.out.println("Invalid weight conversion.");
                return;
            }
        } else {
            System.out.println("Invalid unit type.");
            return;
        }

        // Output the converted value
        System.out.println("Converted value: " + convertedValue + " " + toUnit);
    }
}   

Output:

Enter the value:  100
Enter the unit type (length, temperature, weight):  length
Enter the unit to convert from (e.g., kilometers, celsius, kilograms):  kilometers
Enter the unit to convert to (e.g., miles, fahrenheit, pounds):  pounds
Invalid length conversion.
Enter the value:  100
Enter the unit type (length, temperature, weight):  weight
Enter the unit to convert from (e.g., kilometers, celsius, kilograms):  kilograms
Enter the unit to convert to (e.g., miles, fahrenheit, pounds):  pounds
Converted value: 220.462 pounds

Explanation :

  • Input: The user inputs a value, a unit type (length, temperature, or weight), and the unit to convert from and to.
  • If-Else Structure: Conversions are handled by checking the unit type, and corresponding from-to conversions are implemented.
  • Conversion: Specific conversion formulas are applied based on the unit type and units.
  • Invalid Inputs: If an invalid conversion is entered, the program outputs an error message.
  • Result: The program outputs the converted value in the desired unit.

Solution 2: Using Switch-Case Statements

Code:

import java.util.Scanner;

public class UnitConverterSwitch {
    public static void main(String[] args) {
        // Create a Scanner object to capture user input
        Scanner scanner = new Scanner(System.in);

      // Input the value to be converted
        System.out.print("Enter the value: ");
        double value = scanner.nextDouble();

        // Input the type of conversion (length, temperature, weight)
        System.out.print("Enter the unit type (length, temperature, weight): ");
        String unitType = scanner.next().toLowerCase();

        // Input the unit to convert from
        System.out.print("Enter the unit to convert from (e.g., kilometers, celsius, kilograms): ");
        String fromUnit = scanner.next().toLowerCase();

        // Input the unit to convert to
        System.out.print("Enter the unit to convert to (e.g., miles, fahrenheit, pounds): ");
        String toUnit = scanner.next().toLowerCase();

        // Variable to store the result
        double convertedValue = 0;

        // Switch-case for unit type
        switch (unitType) {
            case "length":
                // Switch-case for length conversion
                switch (fromUnit) {
                    case "kilometers":
                        if (toUnit.equals("miles")) {
                            convertedValue = value * 0.621371;
                        } else {
                            System.out.println("Invalid length conversion.");
                            return;
                        }
                        break;
                    case "miles":
                        if (toUnit.equals("kilometers")) {
                            convertedValue = value * 1.60934;
                        } else {
                            System.out.println("Invalid length conversion.");
                            return;
                        }
                        break;
                    default:
                        System.out.println("Invalid unit for length.");
                        return;
                }
                break;

            case "temperature":
                // Switch-case for temperature conversion
                switch (fromUnit) {
                    case "celsius":
                        if (toUnit.equals("fahrenheit")) {
                            convertedValue = (value * 9/5) + 32;
                        } else {
                            System.out.println("Invalid temperature conversion.");
                            return;
                        }
                        break;
                    case "fahrenheit":
                        if (toUnit.equals("celsius")) {
                            convertedValue = (value - 32) * 5/9;
                        } else {
                            System.out.println("Invalid temperature conversion.");
                            return;
                        }
                        break;
                    default:
                        System.out.println("Invalid unit for temperature.");
                        return;
                }
                break;

            case "weight":
                // Switch-case for weight conversion
                switch (fromUnit) {
                    case "kilograms":
                        if (toUnit.equals("pounds")) {
                            convertedValue = value * 2.20462;
                        } else {
                            System.out.println("Invalid weight conversion.");
                            return;
                        }
                        break;
                    case "pounds":
                        if (toUnit.equals("kilograms")) {
                            convertedValue = value * 0.453592;
                        } else {
                            System.out.println("Invalid weight conversion.");
                            return;
                        }
                        break;
                    default:
                        System.out.println("Invalid unit for weight.");
                        return;
                }
                break;

            default:
                System.out.println("Invalid unit type.");
                return;
        }

        // Output the converted value
        System.out.println("Converted value: " + convertedValue + " " + toUnit);
    }
}  

Output:

Enter the value:  100
Enter the unit type (length, temperature, weight):  temperature
Enter the unit to convert from (e.g., kilometers, celsius, kilograms):  celsius
Enter the unit to convert to (e.g., miles, fahrenheit, pounds):  fahrenheit
Converted value: 212.0 fahrenheit
Enter the value:  100
Enter the unit type (length, temperature, weight):  temperature
Enter the unit to convert from (e.g., kilometers, celsius, kilograms):  kilometers
Enter the unit to convert to (e.g., miles, fahrenheit, pounds):  miles
Invalid unit for temperature.

Explanation:

  • Input: The user provides the value, unit type (length, temperature, or weight), and the units to convert between.
  • Switch-Case Structure: A switch-case structure is used to handle different types of conversions (length, temperature, weight).
  • Nested Switch: Another switch-case block is used to select the correct units for the conversion based on the input.
  • Error Handling: The program handles invalid conversions with error messages.
  • Result: The converted value is displayed to the user in the specified target unit.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-convert-length-temperature-and-weight.php