Computer Science, asked by gaurav7397, 1 year ago

the relative velocity of two trains travelling in opposite directions is calculated by adding their velocities.in case the trains are travelling in the same direction the relative velocity is the difference between their velocities write a program to input the velocities and length of the trains write a menu driven program to calculate the relative velocities and the time taken to cross each other by indicating 1 for same direction and 2 for opposite direction​

Answers

Answered by unknowngirl11
10

Answer:

HERE'S UR ANSWER !!

import java.util.*;

class train

{

public static void main (String args [ ])

{

int v1,v2,l1,l2,len;

float vel,time;

System.out.println("Enter the velocity of first and second train respectively ");

v1=in.nextInt();

v2=in.nextInt();

System.out.println(" Enter the length of both the trains");

l1=in.nextInt();

l2=in.nextInt();

len=l1+l2;

System.out.println("Enter ur choice:1 for for same direction and 2 for opposite direction");

int ch =in.nextInt();

Switch (ch)

{

case 1:

vel=Math.abs (v1-v2);

System.out.println("Relative velocity ="+vel);

time =len/vel;

System.out.println("Time taken ="+time);

break;

case 2:

vel=v1+v2;

System. out.println("Relative velocity ="+vel);

time= len/ vel;

System.out.println("Time taken="+time);

break;

default:

System. out.println("Wrong Choice ");

}

}

HOPE IT HELPS UHH!!!

Answered by akshat78952
0

Answer:

import java.util.*;

class train

{

public static void main (String args [ ])

{

int v1,v2,l1,l2,len;

float vel,time;

System.out.println("Enter the velocity of first and second train respectively ");

v1=in.nextInt();

v2=in.nextInt();

System.out.println(" Enter the length of both the trains");

l1=in.nextInt();

l2=in.nextInt();

len=l1+l2;

System.out.println("Enter ur choice:1 for for same direction and 2 for opposite direction");

int ch =in.nextInt();

Switch (ch)

{

case 1:

vel=Math.abs (v1-v2);

System.out.println("Relative velocity ="+vel);

time =len/vel;

System.out.println("Time taken ="+time);

break;

case 2:

vel=v1+v2;

System. out.println("Relative velocity ="+vel);

time= len/ vel;

System.out.println("Time taken="+time);

break;

default:

System. out.println("Wrong Choice ");

}

}

Explanation:

Similar questions