How to calculate velocity using Java
In this demo, we are going to learn how to calculate the final velocity of an object or a body in Java. We will be taking time, acceleration, displacement, and initial velocity as the parameters.
Calculating the velocity in Java
Formulae to calculate velocity:
There are two general formulae to calculate velocity, v:
- v = u + a*t
- v^2 = u^2 + 2*a*s
where,
u is the initial velocity in m/s,
a is the acceleration in m/s^2,
s is the displacement in meters, and
t is the time taken in seconds.
Steps to write the program:
Step 1: Define variables u, a, s, and t of float data type to represent the parameters.
Step 2: Take the input of initial velocity and acceleration from the user.
Step 3: Ask the user if the third parameter used to calculate the velocity is time or displacement.
Step 4: Depending on the third parameter, use the appropriate formula to calculate the velocity, and store it in a variable v of float data type.
Step 5: Print the value of velocity stored in v.
Java program to calculate velocity of an object:
import java.util.Scanner;
import java.lang.Math;
public class velocity {
public static void main(String[] args){
float v, u, a, s, t;
String c; //Variable to store the choice of third parameter.
/*Getting the values of u and a from the user*/
Scanner input = new Scanner(System.in);
System.out.println("Enter the value of initial velocity: ");
u = input.nextFloat();
System.out.println("Enter the value of acceleration: ");
a = input.nextFloat();
/*Asking the user if the third parameter is time or displacement*/
System.out.println("Press 't' or 'T' to enter the value of time; press 's' or 'S' to enter the value of displacement:");
while(true) {
c = input.next();
if((c.charAt(0) == 't' || c.charAt(0) == 'T') && c.length() == 1) {
/*Since the user chose time, take the value of t and calculate velocity using the first formula*/
System.out.println("Enter the value of time: ");
t = input.nextFloat();
v = u + a * t;
break;
}
else if((c.charAt(0) == 's' || c.charAt(0) == 'S') && c.length() == 1){
/*Since the user chose displacement, take the value of s and calculate velocity using the second formula*/
System.out.println("Enter the value of displacement: ");
s = input.nextFloat();
v = (float) Math.sqrt(u*u + 2*a*s);
break;
}
else {
/*This condition is entered when the user inputs an incorrect character */
System.out.println("Please enter the correct character.");
}
}
System.out.println("Velocity = " + v);
}
}
Output:
Example 1) For, u = 0, a = 10, t = 5,
Enter the value of initial velocity: 0 Enter the value of acceleration: 10 Press 't' or 'T' to enter the value of time; press 's' or 'S' to enter the value of displacement: t Enter the value of time: 5 Velocity = 50.0
Example 2) For, u = 2.4, a = 5, s = 10,
Enter the value of initial velocity: 2.4 Enter the value of acceleration: 5 Press 't' or 'T' to enter the value of time; press 's' or 'S' to enter the value of displacement: s Enter the value of displacement: 10 Velocity = 10.283968
Leave a Reply