program to find the displacement

Calculate the Displacement using initial velocity, acceleration and, time taken 


The data like(initial velocity, acceleration and, time taken ) needed to calculate simple interest can be initialized with the default value inside the class and also can be taken from the user. Here 'Scanner' class, the built-in class inside the 'util' package of 'java' project is imported to get value from the keyboard.

import java.util.Scanner; //Calling Scanner class
public class Displacement
{
    //Declaring Variables
    double u; //Initial velocity
    double t; //Time taken
    double a; //Acceleration
    public static void main(String []args)
    {
          Scanner obj=new Scanner(System.in); //Creating object of Scanner class                                    
          System.out.println("Enter initial velocity in m/s"); //Instruction for user
          double a=obj.nextInt()//Getting value from user
          System.out.println("Enter time taken in second"); //Instruction for user
          double b=obj.nextInt()//Getting value from user
          System.out.println("Enter Acceleration m/s^2"); //Instruction for user
          double c=obj.nextInt()//Getting value from user
          Displacement val= new Displacement(); //Creating and Initializing Constructor
          val.calc_displacement(a, b, c); //Calling calc_displacement method
   
     }
     public void calc_displacement(double u, double t,double a) //Creating parameterized method
     {
          double s= (u*t+(1/2)*a*(Math.pow(t,2))); //Processing
          System.out.println("Distance covered by moving bodies is "+s+" meter"); //Printing Result
      }
}


Output:
    Enter initial velocity in m/s
    6
    Enter time taken in second
    25
    Enter Acceleration m/s^2
    8
    Distance covered by moving bodies is 150.0 meter

Comments

Popular posts from this blog

Java program using Scanner Class

Java Program to make a comparison between numbers

Calculate the area of Rectangle Using Constructor Overloading rules