Monday, 1 December 2014

// The "CalcMotion" class.

// Create a CalcMotion application program that prompts the user for the total
// distance (in metres), the time elapsed (in seconds) and uses these values to
// calculate the velocity (speed) of travel (in m/sec) based on this formula:
// velocity = distance / time elapsed.
// Make the output appear meaningful and attractive (include proper units of
// measurement in the answer).
import java.awt.*;
import hsa.Console;

public class CalcMotion
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        double distance, timeElapsed, velocity;

        c.print ("Enter the value of the total distance travelled (in m): ");
        distance = c.readDouble ();

        c.print ("Enter the value of the time elapsed (in seconds): ");
        timeElapsed = c.readDouble ();


        if (timeElapsed > 0)
        {
            // calculate velocity of your trip (in m / sec)
            velocity = distance / timeElapsed;

            c.println ("You travelled " + distance + " metres in " + timeElapsed
                    + " seconds, which means your velocity was ");
            c.println (Math.round (velocity) + " m/sec");

        }
        else
        {
            c.println ("ERROR: Velocity can only be calculated if the "
                    + "time elapsed is a positive value");
        }

    } // main method
} // CalcMotion class

No comments:

Post a Comment