Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Locale;

public class Main {
    /***
     * Fetch the estimate travel time to the indicated target
     * @param distance - how far to the target
     * @param speed - how fast we are moving
     * @param bearing - direction to target
     * @param heading - direction of movement
     * @return String - "HH:MM" time to the target
     */
    public static String calculateEte(double distance, double speed, double bearing, double heading) {

        // Fetch the eteRaw value
        int eteRaw = fetchRawEte(distance, speed, bearing, heading);

        // If no speed or an invalid eteRaw, then return the empty display value
        if (0 == speed || eteRaw == -1) {
            return "--:--";
        }

        // Break the eteRaw out into hours and minutes
        int eteHr = eteRaw / 100;
        int eteMin = eteRaw % 100;

        // Hours greater than 99 are not displayable
        if (eteHr > 99) {
            return "XX:XX";
        }

        // Format the hours and minutes en router
        String hr = String.format(Locale.getDefault(), "%02d", eteHr);
        String min = String.format(Locale.getDefault(), "%02d", eteMin);

        // BUit the string for return
        return hr + ":" + min;
    }

    /***
     * Fetch the raw estimated time enroute given the input parameters
     * @param distance - how far to the target
     * @param speed - how fast we are moving
     * @param bearing - direction to target
     * @param heading - direction of movement
     * @return int value of HR * 100 + MIN for the ete, -1 if not applicable
     */
    private static int fetchRawEte(double distance, double speed, double bearing, double heading) {
        // We can't assume that we are heading DIRECTLY for the destination, so 
        // we need to figure out the multiply factor by taking the COS of the difference
        // between the bearing and the heading.
        double angDif = angularDifference(heading, bearing);

        // If the difference is 90 or greater, then ETE means nothing as we are not
        // closing on the target
        if (angDif >= 90)
            return -1;

        // Calculate the actual relative speed closing on the target
        double xFactor = Math.cos(angDif * Math.PI / 180);
        double eteTotal = distance / (speed * xFactor);

        // Break that down into hours and minutes
        int eteHr = (int) eteTotal;
        int eteMin = (int) Math.round((eteTotal - (double) eteHr) * 60);

        // account for the minutes being 60
        if (eteMin >= 60) {
            eteHr++;
            eteMin -= 60;
        }

        // Return with our estimate
        return eteHr * 100 + eteMin;
    }

    /** Calculate the absolute angular difference between the two headings
     * 
     * @param hdg angle 1 in degrees (typically the heading)
     * @param brg angle 2 in degrees (typically the bearing)
     * @return difference between hdg and brg in degrees
     */
    public static double angularDifference(double hdg, double brg) {
        double absDiff = Math.abs(hdg - brg);
        if (absDiff > 180) {
            return 360 - absDiff;
        }
        return absDiff;
    }
}