Define method UnitConverter() that takes an input a double representing a distan
ID: 3611106 • Letter: D
Question
Define method UnitConverter() that takes an inputa double representing a distance and a String specifying aunit("inch", "foot", "yard"). Method UnitConverter() print theequivalent of the distance in the given unit converted intocentimetersThe rules for converting the inches, foots and yardsinto centimetres are as follows:
1 inch= 2.54 centimetres
1 foot = 30.48 centimetres
1 yard =91.44 centimetres
Method UnitConverter() prints the results as follows:
2.0 foot = 60.96 cm
1.5 yard = 137.16 cm
Then in the main() method call method UnitConverter() with thefollowing values:
2.0 foot
1.5 yard
a distance value and a unit value of yourchoice
Explanation / Answer
import java.io.*; import java.util.*; import java.lang.*; public class Converter { public static void UnitConverter(double distance, String units) { if(units.equals("inch")) { System.out.println(distance +" " + units + " = " + (distance * 2.54) + " cm"); } else if(units.equals("foot")) { System.out.println(distance +" " + units + " = " + (distance * 30.48) + " cm"); } else if(units.equals("yard")) { System.out.println(distance +" " + units + " = " + (distance * 91.44) + " cm"); } else { System.out.println("EnteredWrong Units"); } }//end of UNitConverter Fucntion public static void main(String [] arg) { System.out.println(); UnitConverter(2.0, "foot"); System.out.println(); UnitConverter(1.5, "yard"); System.out.println(); }//end of main }//end of class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.