In the previous article, we have seen Java Program to Convert Time From 24-hour Format to 12-hour Format
In this article we are going to see how calculate the total distance if the speed is given using Java programming language.
Java Program to Calculate the Total Distance if the Speed is Given
Before jumping into the program directly, let’s know first how we can find total distance if speed is given.
According to the Equation of motion, we have
Speed= distance / time Distance= speed X time
Let’s see different ways to find total distance if the speed is given .
Method-1:Java Program to Calculate the Total Distance if the Speed is Given By Using Static Input Value
Approach:
- Initialize variables for speed and total time taken.
- Multiply speed and time.
- Print the result.
Program:
public class Main { public static void main(String[] args) { Double speed = 60.0; Double time = 2.0; double distance = speed * time; System.out.println("Distance travelled is: " + distance + "kms"); } }
Output: Distance travelled is: 120.0kms
Method-2: Java Program to Calculate the Total Distance if the Speed is Given By using User Input Value
Approach:
- Create Scanner class object
- Take user input for speed and total time taken.
- Multiply speed and time.
- Print the result.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); //taking input of speed and total time System.out.print("Enter the speed in kms: "); Double speed = sc.nextDouble(); System.out.print("Enter time taken hrs: "); Double time = sc.nextDouble(); double distance = speed * time; System.out.println("Distance travelled is: " + distance + "kms"); } }
Output: Enter the speed in kms: 60 Enter time taken hrs: 0.5 Distance travelled is: 30.0kms
Method-3: Java Program to Calculate the Total Distance if the Speed is Given By using User Defined Method
Approach:
- Create Scanner class object
- Take user input for speed and total time taken.
- Call the method
findDistance(
) and pass speed and total time as parameter. - Inside method multiply speed and time.
- Print the result.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); //taking input of speed and total time System.out.print("Enter the speed in kms: "); Double speed = sc.nextDouble(); System.out.print("Enter time taken hrs: "); Double time = sc.nextDouble(); //calling findDistance() method to find total distnace findDistance(speed,time); } public static void findDistance(double speed,double time) { //calculating distance by multiplying speed and total time taken double distance = speed * time; System.out.println("Distance travelled is: " + distance + "kms"); } }
Output: Enter the speed in kms: 65 Enter time taken hrs: 1.5 Distance travelled is: 97.5kms
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: