TravelTime.java.html

  1  import java.util.Scanner;
  2  
  3  public class TravelTime
  4  {
  5     public static void main(String[] args)
  6     {      
  7        Scanner in = new Scanner(System.in);
  8        System.out.print("Distance to item along x-axis: ");
  9        double xDistance = in.nextDouble();
 10        System.out.print("Distance to item along y-axis: ");
 11        double yDistance = in.nextDouble();
 12        System.out.print("Length of segment 1: ");
 13        double segment1Length = in.nextDouble();
 14        System.out.print("Speed along segment 1: ");
 15        double segment1Speed = in.nextDouble();
 16        System.out.print("Speed along segment 2: ");
 17        double segment2Speed = in.nextDouble();
 18  
 19        double segment1Time = segment1Length / segment1Speed;
 20        double segment2Length = Math.sqrt(Math.pow(xDistance, 2)
 21           + Math.pow(yDistance - segment1Length, 2));
 22        double segment2Time = segment2Length / segment2Speed;
 23        double totalTime = segment1Time + segment2Time;
 24        System.out.println("Total time: " + totalTime);
 25     }
 26  }