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 classpublic class Displacement{//Declaring Variablesdouble u; //Initial velocitydouble t; //Time takendouble a; //Accelerationpublic static void main(String []args){Scanner obj=new Scanner(System.in); //Creating object of Scanner classSystem.out.println("Enter initial velocity in m/s"); //Instruction for userdouble a=obj.nextInt(); //Getting value from userSystem.out.println("Enter time taken in second"); //Instruction for userdouble b=obj.nextInt(); //Getting value from userSystem.out.println("Enter Acceleration m/s^2"); //Instruction for userdouble c=obj.nextInt(); //Getting value from userDisplacement val= new Displacement(); //Creating and Initializing Constructorval.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))); //ProcessingSystem.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
Post a Comment