import java.util.Scanner;
import java.text.NumberFormat;
public class WeightConverter
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String prompt = "Enter weight in lbs: ";
boolean isValid = false;
double weightInPounds = 0.0;
while (isValid == false)
{
weightInPounds = getDouble(sc, prompt);
if (weightInPounds > 0)
isValid = true;
else
System.out.println("Weight must be greater than 0.");
}
double weightInKilos = weightInPounds / 2.2;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String message = weightInPounds + " lbs\nequals\n"
+ nf.format(weightInKilos) + " kgs\n";
System.out.print(message);
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println
("Error! Invalid decimal value. Try again.");
}
sc.nextLine();
}
return d;
}
}
a. 53 kgs
b. 54 kgs
c. 53.64 kgs
d. 53.6363 kgs
Answer: 53.64 kgs
import java.text.NumberFormat;
public class WeightConverter
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String prompt = "Enter weight in lbs: ";
boolean isValid = false;
double weightInPounds = 0.0;
while (isValid == false)
{
weightInPounds = getDouble(sc, prompt);
if (weightInPounds > 0)
isValid = true;
else
System.out.println("Weight must be greater than 0.");
}
double weightInKilos = weightInPounds / 2.2;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String message = weightInPounds + " lbs\nequals\n"
+ nf.format(weightInKilos) + " kgs\n";
System.out.print(message);
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println
("Error! Invalid decimal value. Try again.");
}
sc.nextLine();
}
return d;
}
}
If the user enters 118 at the console prompt, what is the third line of the resulting console display?
a. 53 kgs
b. 54 kgs
c. 53.64 kgs
d. 53.6363 kgs
Answer: 53.64 kgs
Learn More :
Mobile App
- What is the most common file type of media supported for audio playback on the Android?
- What is the first GridView control in a project named by default?
- What is the TextView property for backgrounds?
- What invokes a scheduled Timer?
- What does the splash screen provide time for Android to do?
- What category of the Palette does the control GridView sit in?
- What can be described as the series of actions from the beginning of an Activity to its end?
- Using controls such as the GridView and Spinner, what binds specific types of data and displays that data in a particular layout?
- On an Android, what are 1000 milliseconds equivalent to in seconds?
- Each of the following is a common state for the MediaPlayer class except for which one?
- What is printed to the console after the code that follows is executed?
- What happens when the code that follows is executed?
- To use the binarySearch method of the Arrays class on an array of ints, you must first
- The length of the array that follows is int[] grades = new int[4];
- The array that follows is an array of double[] grades = new double[3];
- How many rows are in the array that follows? Rental[][] transactions = new Rental[7][3];
- What is the length of the bestTimes array?
- An enhanced for loop can only be used
- Which of the following is not a reason to create a package?
- Which of the following is a valid Javadoc comment?
- Which of the following is a benefit of using Javadoc comments?
- Where must you code the Javadoc comment for a method?
- To include a class in a package, you
- In what type of file is Javadoc documentation stored?
- A subclass inherits