Computer Science, asked by stpbhabani, 5 hours ago

write a menu driven program in java to caculate the relative velocity and the time taken to cross two train o each other

Answers

Answered by pps147265
0

Answer:

java.util.Scanner; public class KboatTrain { 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