Problem

The United States uses the imperial system of weights and measures, which means that there are many different, seemingly arbitrary units to measure distance. There are 12 inches in a foot, 3 feet in a yard, 22 yards in a chain, and so on.

Create a data structure that can efficiently convert a certain quantity of one unit to the correct amount of any other unit. You should also allow for additional units to be added to the system.

Examples

Example 1

Solution

To solve this problem, we can create a data structure that captures the relationships between different units and allows efficient conversion between them.

Method 1 - Using Hashmap

This class will store conversion factors between different units in a map and provide methods to convert between units and add new units.

  • We use a HashMap to store conversion factors from different units to a base unit (inches in this case).
  • The key in the map is the unit name (e.g., “inch”, “foot”), and the value is the conversion factor to inches.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class ImperialUnitConverter {
    // Map to store conversion factors for each unit to a base unit (e.g., inches).
    private Map < String, Double > conversionFactors;

    public ImperialUnitConverter() {
        conversionFactors = new HashMap<>();

        // Initialize with some known conversions
        addUnit("inch", 1.0);
        addUnit("foot", 12.0); // 1 foot = 12 inches
        addUnit("yard", 36.0); // 1 yard = 3 feet = 36 inches
        addUnit("chain", 792.0); // 1 chain = 22 yards = 792 inches
    }

    public void addUnit(String unit, double factorToInch) {
        conversionFactors.put(unit, factorToInch);
    }

    public double convert(double quantity, String fromUnit, String toUnit) {
        if (!conversionFactors.containsKey(fromUnit) || !conversionFactors.containsKey(toUnit)) {
            throw new IllegalArgumentException("Unknown unit: " + fromUnit + " or " + toUnit);
        }

        double factorFrom = conversionFactors.get(fromUnit);
        double factorTo = conversionFactors.get(toUnit);

        // Convert the quantity to the base unit (inches) first, then to the target unit
        double quantityInInches = quantity * factorFrom;
        return quantityInInches / factorTo;
    }

    public static void main(String[] args) {
        ImperialUnitConverter converter = new ImperialUnitConverter();

        // Examples of conversion
        double feetToYards = converter.convert(6, "foot", "yard"); // 6 feet to yards
        System.out.println("6 feet is " + feetToYards + " yards");

        double yardsToChains = converter.convert(44, "yard", "chain"); // 44 yards to chains
        System.out.println("44 yards is " + yardsToChains + " chains");

        // Adding a new unit and converting
        converter.addUnit("mile", 63360.0); // 1 mile = 63360 inches

        double milesToInches = converter.convert(1, "mile", "inch"); // 1 mile to inches
        System.out.println("1 mile is " + milesToInches + " inches");

        double chainsToMiles = converter.convert(88, "chain", "mile"); // 88 chains to miles
        System.out.println("88 chains is " + chainsToMiles + " miles");
    }
}