Working with if Suppose that you are traveling to the UK where they use Kilogram
ID: 672796 • Letter: W
Question
Working with if
Suppose that you are traveling to the UK where they use Kilograms and grams, but your brain thinks in pounds and ounces, so you want the computer to perform the conversion for you in either direction. You want the program to show you a menu of choices, and ask you which operation you want. The user responds. Based on the user’s choice the program calculates a different answer. This is a natural place to use if statements to compare the user's response to the choices supplied. The program also has to test whether or not the user's answer is outside the known list of choices. Write a commented program to convert between pounds and Kilograms in either direction Give the user two choices. Convert weights: 1. Pounds to Kilograms and grams 2. Kilograms to pounds and ounces After displaying the menu, ask the user which they want: Which conversion do you want? Then ask the user for what weight value should be converted. What weight value do you want converted? If the user chooses 1, then convert the pounds to Kilograms and grams, else if the user chooses 2 convert the kilograms to pounds and ounces, else if the user choice is anything else, tell the user their answer is invalid, and ask again, and get a better choice. Display the answer with the correct label on it. Input: The input to your program will be from the keyboard (using Scanner). Output: The output of your program should be to the screen, and include the correct answers. Run: it three times, to be sure all possible values of the if logic work correctly TURN IN 1. your commented source code 2. show the output of a successful compile with the different outputs of your program executions.
test your nameKiloLb.java program as you have done for all the previous labs. I wish to see what values you are testing when you are determining if your program works correctly. Below, outline the computers output and your input for 4 test cases. Make sure the test cases account for all of the possibilities that the user could enter. Write down four different test cases using values that differ from the example as well as how your program outputted information. Example: Convert weights: 1. Pounds to Kilograms and grams 2. Kilograms to pounds and ounces Which conversion do you want? 2 What weight value do you want converted? 1 1 kg is 2 pounds 3 ounces
Explanation / Answer
working java code
class convert
{
public static double poundstokg( double weight)
{
double kg;
kg= 0.453 * weight;
return kg;
}
public static double kgtopounds( double w)
{
double pounds;
pounds= 2.20462 * w;
return pounds;
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
double res;
System.out.println("Convert weights: 1. Pounds to Kilograms and grams 2. Kilograms to pounds and ounces Which conversion do you want?");
int op=sc.nextInt();
if (op == 1)
{
double weight = sc1.nextDouble();
res = poundstokg(weight);
System.out.println("Weight in kg is " + res);
}
else
{
double weight = sc1.nextDouble();
res = kgtopounds(weight);
System.out.println("Weight in pounds is " + res);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.