0

I was wondering if I could get some help trying to create a simple math formula. I recently was given an interview to work as a tier1 programmer and was asked to make a program. I made the whole thing perfectly fine but what made me fail the interview was that I need to create a math formula to convert different units of measure to other units of measure. This is easy, but what made it hard was that I needed to convert these different units of measure based off of the lengths given to me in feet. This is really bugging me because the guy said that I could just made one equation that could been used for all of the conversions to the different units of measurement. So, the current units of measurement that were given contained their original units of measurement and their measurement in terms of feet. The formula has to utilize the different lengths in terms of feet to do these conversions. Other wise, I would got the job by just dividing and multiplying everything to get my answer. The different units of measure are as follows:

inch,.083333    
fathom,6    
foot,1    
furlong,660    
kilometer,3281.5    
meter,3.2815    
mile,5280    
rod,16.5    
yard,3 

The numbers after the comma are the lengths in terms of feet for that unit of measure. I have been staring at this for hours, but I cannot figure out an equation to use for all the lengths. For those that give me help, I really do appreciate it, I am beginning to feel defeated with this.

For programing sake, I used X as the original units of measurement, Y as the desired units or measurement, and Z for the inputted length that needed to be converted.

shA.t
  • 16,580
  • 5
  • 54
  • 111

2 Answers2

0

You can do it like this:

import java.util.HashMap;

public class ConvertLength {
  private static HashMap<String,Double> lengthInFeet
    = new HashMap<String,Double>();

  static {
    lengthInFeet.put("inch", 0.083333);
    lengthInFeet.put("inches", 0.083333);
    lengthInFeet.put("fathom", 6.0);
    lengthInFeet.put("fathoms", 6.0);
    lengthInFeet.put("foot", 1.0);
    lengthInFeet.put("feet", 1.0);
    lengthInFeet.put("furlong", 660.0);
    lengthInFeet.put("furlongs", 660.0);
    lengthInFeet.put("kilometer", 3281.5);
    lengthInFeet.put("kilometers", 3281.5);
    lengthInFeet.put("meter", 3.2815);
    lengthInFeet.put("meters", 3.2815);
    lengthInFeet.put("mile", 5280.0);
    lengthInFeet.put("miles", 5280.0);
    lengthInFeet.put("rod", 16.5);
    lengthInFeet.put("rods", 16.5);
    lengthInFeet.put("yard", 3.0);
    lengthInFeet.put("yards", 3.0);
  };

  public static double convert(double length, String from, String to) {
    return length * lengthInFeet.get(from) / lengthInFeet.get(to);
  }

  public static void main(String[] args) {
    double length = Double.parseDouble(args[0]);
    String from = args[1];
    String to = args [2];

    System.out.println(length + " " + from + " = "
               + convert(length,from,to) + " " + to);
  }
}
Edward Doolittle
  • 4,002
  • 2
  • 14
  • 27
0

Practical issues aside, you could use an equation like this:

length = αvwᵀ

Here, α is the length in feet, v is a vector that contains the conversion factors (i.e. [0.08333, 6, 1, 660, …]) and w is a vector like [0, …, 1, …, 0] with the value one at the index that corresponds to the desired unit.

tsnorri
  • 1,966
  • 5
  • 21
  • 29