tekman22 tekman22 - 21 days ago 8
Java Question

Java - convert any unit to any unit

I am trying to make a method that will convert any unit to any larger unit. I need to, I'm guessing provide parameters for each input, so I'm guessing it needs to ask the user parameters for..startingNum, conversionFactor, outPutNum, but I'm unsure where to begin. I already made one with inches:

public Convertor()
{
inches=0;
feet=0;
yards=0;
leftoverInches=0;
}

/**
* Mutator method to convert inches to feet
*/
public void convertValuesInchtoFeet(int anyInches)
{
inches=anyInches;
feet=anyInches/12;
leftoverInches= anyInches%12;
System.out.println(inches+" inches = " +feet+" feet.");
System.out.println("There are " +leftoverInches +" leftover inches");

}


Please help! Thanks.

Answer

Any unit to any unit is a bit vague, but generally you need to provide: a) input number b) input unit c) output unit

Assuming you want to make a function that does general conversion between feet, inches and yards.

I'd take a look at the TimeUnit API and see if you get any ideas on a different way of doing it. Its made using enumerations with methods in them, but I don't know how familiar you are with Enums.

Oh yeah, TimeUnit is used like this: TimeUnit.SECONDS.toMillis(1); // Returns 1000