Write a full program with a function named convert that takes two integer argume
ID: 3726646 • Letter: W
Question
Write a full program with a function named convert that takes two integer arguments corresponding to kilograms and grams, passes them to a function named calculate that calculates the amount in pounds and returns the corresponding floating point value in pounds back to convert for display. 1 kg 2.20462 lbs, and therefore 1g 00220462 Ibs. This must be a full program that uses two auxiliary functions, and a sample run should look something like this: 3. Welcome to the converter. printed from the main function Enter-1 to quit, otherwise enter the kg and g amount to convert: 9 800 printed from convert 9 kg 800g is 21.605276 lbs. printed from convert() Enter-1 to quit, otherwise enter the KG amount to convert: 3 200 printed from convert() 3 kg 200g is 7.054784 lbst printed from convert() Enter-1 to quit, otherwise enter the KG amount to convert: -1 printed from convert() Goodbye. printed from convert() or main(), your choice ** "Don't forget to use three functions: main(), convert(), and calculate().***Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class Converter {
private static final double KG_TO_LBS = 2.20462;
private static final double G_TO_LBS = 0.00220462;
public static void main(String[] args) throws IOException {
System.out.println("Welcome to the converter.");
convert();
}
public static void convert() {
boolean exit = true;
while (exit) {
System.out
.print("Enter - 1 to quit, otherwise enter the kg and g amount to convert:");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String input = "";
try {
input = br.readLine();
} catch (IOException e1) {
}
try {
String i[] = input.split(" ");
int kg = 0;
try {
kg = Integer.parseInt(i[0]);
} catch (Exception e) {
}
int g = 0;
try {
g = Integer.parseInt(i[1]);
} catch (Exception e) {
}
// System.out.println(kg +" "+g);
if (kg == 1) {
exit = false;
System.out.println("Goodbye.");
break;
} else {
if (!"".equalsIgnoreCase(input)) {
double lbs = calculate(kg, g);
DecimalFormat df = new DecimalFormat("#.000000");
System.out.println(kg + " kg " + g + "g is "
+ df.format(lbs) + " lbs.");
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
public static double calculate(int kg, int g) {
return (kg * KG_TO_LBS) + (g * G_TO_LBS);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.