Sunday, August 2, 2015

5. Mass and Weight

Write a program that asks the user to enter an object's mass, and then calculate its weight. If the object weighs more than 1,000 Newtons, display a message indicating that it is too heavy. If the object weighs less than 10 Newtons, display a message indicating that the object is too light.
import java.util.Scanner;

public class Weight                                  //class header
{
    public static void main(String[] args)     //method header
    {

     //Info on what this program does.
     System.out.println("Welcome");
     System.out.println("This program calculates an object's weight ");
     System.out.println();

     //Getting input from the user
     Scanner input = new Scanner (System.in);
     System.out.print("Enter an object's mass : ");                    //prompt to the user
     double mass = input.nextDouble();                                    //storing it

     //Calculation
     double weight = mass * 9.8;
   
     //Results
     String result = " ";                                                               //I want to initialize it (habit)
     if (weight > 1000)
     {
           result = "Too heavy";
     }
     else if (weight < 10)
    {
          result = "Too light";
     }

    //OUTPUT
    System.out.println(result);

    }                    //end of method


}                     //end of class






No comments:

Post a Comment