In: Computer Science
Task 3. Falling distance
When an object is falling because of gravity, the following formula can be used to determine
the distance the object falls in a specific time period:
d=(1/2)gt2
The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the
amount of time, in seconds, that the object has been falling.
3.1. Create a method: FallingDistance
Parameters: t, object’s falling time (in seconds). t may or may not be an integer value!
Return value: the distance, in meters, that the object has fallen during that time interval
Calculations: Use Math.Pow() to calculated the square in the formula
3.2. Demonstrate the method by calling it from a loop that passes the values 1 through 20 as arguments, and displays each returned value.
** JAVA PROGRAM **
Hi,
Please find below code as per your requirement.
Let me know if you have any doubt/concerns in this via comments.
Hope this answer helps you.
Thanks.
/***************JAVA CODE***************/
/**************************************/
public class FallingDistanceCalculator {
/**
* This is driver method which test the functionality of fallingDistance method
* @param args
*/
public static void main(String[] args) {
//Iterating 20 times and calling fallingDistance method with i as parameter and printing output
for (int i = 1; i <= 20; i++) {
System.out.println("Falling distance "+fallingDistance(i)+ " for falling time "+i+" seconds.");
}
}
/**
* This method calculates falling distance in meters for given falling time in seconds
* @param fallingTime
* @return
*/
public static double fallingDistance(double fallingTime) {
double g= 9.8;
double half = 0.5; //here 1/2 converted to 0.5
//here we are applying formula d = (1/5) * g * falling time power 2
double fallingDistanceInMeters = (half * g) * Math.pow(fallingTime, 2);
return fallingDistanceInMeters;
}
}
/*****************Ouput*******************/