Tuesday, November 22, 2011

Calculating the distance travelled by a car at different time intervals


PROBLEM
Write a program to calculate the distance travelled by a car at different time intervals. The initial velocity of the car is 36 km/hr and the constant acceleration is 5 m/s2.

The formula to calculate distance is:

Distance Travelled = u*t+((a*t*t)/2) where,
u = initial velocity of the car (36 km/hr)
a = acceleration of the car (5 m/s2)
t = time duration in seconds

The program should accept 2 time intervals as the input (one time interval per line) and print the distance travelled in meters by the car (one output per line).

Definitions:
————
1 kilometer = 1000 meters
1 hour = 3600 seconds

Let us suppose following are the inputs supplied to the program

10
8

Then the output of the program will be

350
240

Solution in simple java code


import java.util.Scanner;
class problem4{
public static void calculateDistance(int t1,int t2){
int u = 10; // in m/s
int a = 5; // in m/s2
int distanceTravel1 = u*t1+((a*t1*t1)/2);
int distanceTravel2 = u*t2+((a*t2*t2)/2);
System.err.println(distanceTravel1);
System.err.println(distanceTravel2);
}
public static void main(String args[]){
int firstValue;
int secondValue;
   Scanner in = new Scanner(System.in);
    firstValue = in.nextInt();
secondValue = in.nextInt();
calculateDistance(firstValue,secondValue);
}
}

Note: Input given via Console input(using scanner class).
After run this code via command prompt, you should type two values (one value per line) and then enter. now you can see the output.

5 comments:

  1. Your blog provides us a very great information. Its helpful to this topic for find some result.

    ReplyDelete
  2. Illustrations or photos entirely on yuor web blog even in the event getting need to know fast a dash of number submits. Gratifying technique for long-run forthcoming, I will be book-marking during the time turned out to be a covering conclusion happens inches all the way up inches. UKdistance

    ReplyDelete
  3. Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. entfernung

    ReplyDelete