Computer Science, asked by shalu3954, 10 months 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.​


shalu3954: but no answer
shalu3954: 10 ICSE
shalu3954: 2 he questions hai baki Sab complete hai
shalu3954: thanks u sir
sswaraj04: welcome
sswaraj04: i m not sir
sswaraj04: just 3 year senior from you
shalu3954: okay friend
shalu3954: good night
sswaraj04: gn

Answers

Answered by sswaraj04
53

Answer:

import java.util.Scanner;

import java.lang.*;

public class Main {

 public static void main(String[] args) {

     Scanner sc=new Scanner(System.in);

  System.out.println("Enter velocity of train1");

  int v1=sc.nextInt();

  System.out.println("Enter velocity of train2");

  int v2=sc.nextInt();

  System.out.println("Enter length of train1");

  int l1=sc.nextInt();

  System.out.println("Enter length of train2");

  int l2=sc.nextInt();

  int len=l1+l2;

  System.out.println("Enter choice 1.trains are travelling in same direction 2.trains are travelling in opposite direction");

  int ch=sc.nextInt();

  switch(ch)

  {

      case 1:

          float vel=Math.abs(v1-v2);

          System.out.println("Relative velocity is (m/sec)"+vel);

          float time=len/vel;

          System.out.println("Time taken to cross each other is(sec) "+time);

          break;

       case 2:

           vel=v1+v2;

          System.out.println("Relative velocity is (m/sec) "+vel);

          time=len/vel;

          System.out.println("Time taken to cross each other is (sec) "+time);

          break;

       default:

          System.out.println("wrong choice");

       

  }

 

 }

}

Explanation:

Hope it helps :-)

Answered by bharath412
2

Answer:

import java.util.Scanner;

public class Train

{

public static void main(String args[ ])

{

Scanner in = new Scanner(System.in);

System.out.println("1. Trains travelling in same direction");

System.out.println("2. Trains travelling in opposite direction");

System.out.print("Enter your choice: ");

int choice = in.nextInt();

System.out.print("Enter first train velocity: ");

double speed1 = in.nextDouble();

System.out.print("Enter first train length: ");

double len1 = in.nextDouble();

System.out.print("Enter second train velocity: ");

double speed2 = in.nextDouble();

System.out.print("Enter second train length: ");

double len2 = in.nextDouble();

double rSpeed = 0.0;

switch(choice) {

case 1:

rSpeed = Math.abs(speed1 - speed2);

break;

case 2:

rSpeed = speed1 + speed2;

break;

default:

System.out.println("Wrong choice! Please select from 1 or 2.");

}

double time = (len1 + len2) / rSpeed;

System.out.println("Relative Velocity = " + rSpeed);

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

}

}

Similar questions