virtualideaz

Weight.java

Nov 25th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. 1. Write a java program that prompts the user to enter a weight in pounds and ounces and output the weight expressed in kilograms and grams. One pound is equals to 0.453592 kilograms. Name the program as Weight.java
  2.  
  3. Code :
  4.  
  5. import java.util.Scanner;
  6. public class Weight {
  7.  
  8. static Scanner sc = new Scanner(System.in);
  9. public static void main(String [] args)
  10.         {
  11.             //we need variables to hold values that the user will input
  12.  
  13.             double pounds;
  14.             double ounces;
  15.             double kg;
  16.             double g;
  17.             double kilograms;
  18.             double grams;
  19.  
  20.            //variables are created
  21.            //the program will ask the user to input number for pounds
  22.  
  23.             System.out.print("Enter pounds : ");
  24.             pounds = sc.nextDouble();
  25.           //pounds has been declared
  26.  
  27.           //the program will now ask the user to input number ounces
  28.             System.out.print("Enter ounces : ");
  29.             ounces = sc.nextDouble();
  30.           //ounces has been declared
  31.  
  32.           //set conversion table : pounds to kilograms
  33.             kg = 0.453592;
  34.             g = 0.02845;
  35.  
  36.           //conversion computation
  37.             kilograms = pounds*kg;
  38.             grams = ounces*g;
  39.  
  40.           //display output
  41.             System.out.println("Kilograms : " +kilograms);
  42.             System.out.println("Grams : " +grams);
  43.  
  44.         }
  45. }
Add Comment
Please, Sign In to add comment