Tuesday, 2 December 2014

// The "WorkAndPower2" class.

// Create a WorkAndPower2 application to prompt the user to enter the work
// required (in joules) and the time elapsed (in seconds) and calculates the power
// generated (in Watts) based on this formula:  power = work / time.
// Make the output appear meaningful and attractive (include proper units of
// measurement in the answer).


import java.awt.*;
import hsa.Console;

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

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

        double work, power, time;

        c.print ("Enter the work required (in joules): ");
        work = c.readDouble ();

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

        if (time > 0)
        {
            // now, calculate the power generated (in Watts)
            power = work / time;

            c.print ("The power generated is " + power + " Watts");
            c.println (" based on the work required: " + work + " joules");
            c.println ("and the time elapsed: " + time + " seconds");
        }
        else
        {
            c.print ("The time: " + time + " seconds must be a positive value");
            c.println (" in order to perform division");
        }

    } // main method
} // WorkAndPower2 class

No comments:

Post a Comment