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
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!!!
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: