Write a unit conversion program that asks users to identify the unit from which
ID: 3640686 • Letter: W
Question
Write a unit conversion program that asks users to identify the unit from which they want to convert and the unit to which they want to convert. Legal units are in, ft, mi, mm, cm, m, andkm. Declare two objects of a class UnitConverter that convert between meters and a given unit.Convert from:
in
Convert to:
mm
Value:
10
10 in = 254 mm
For this assignment, create the following 2 classes:
class UnitConverter
with the following methods:
public UnitConverter(String unit) (Constructor)
public double toMeters(double measurement)
public double fromMeters(double measurement)
and
class ConversionCalculator
with the following methods:
public static void main(String[] args)
Explanation / Answer
public class UnitConverter { final double INCH_TO_METER = 0.0254; final double FOOT_TO_METER = 0.305; final double MILE_TO_METER = 1609; static double KILOMETERS = 0.001; static double CENTIMETERS = 100; static double MILES = 0.00062; static double INCHES = 39.37; String fromUnit , toUnit; private double val , meters, aconverted; /** Constructs a unit converter to convert between a unit and meters @param unit the unit used by this converter */ public UnitConverter(String vfromUnit) { vfromUnit=fromUnit; } /** Converts a measurement to meters. @param measurement a measurement in the units of this converter @return the equivalent meters */ public double toMeters(double measurement) { if(toUnit=="miles") { aconverted = 1/MILES * val; } else if(toUnit=="centimeters") { aconverted = 1/CENTIMETERS * meters; } else if(toUnit=="inches") { aconverted = 1/INCHES * meters; } else aconverted = 1/KILOMETERS * meters; return aconverted; } /** Converts a measurement from meters. @param measurement the measurement to convert from meters @return the equivalent value in the unit of this converter */ public double fromMeters(double meters) { if(fromUnit=="miles") { meters = MILES*val ; } else if(fromUnit=="inches") { meters = INCHES*val; } else if(fromUnit=="centimeters") { meters = CENTIMETERS*val; } else meters = KILOMETERS*val; return meters; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.