01 Ipo Programming Examples

Embed Size (px)

Citation preview

  • 8/10/2019 01 Ipo Programming Examples

    1/4

    IPO Programming Examples Page 1

    IPO PROGRAMMING EXAMPLES

    Convert Liters to U.S. GallonsWrite a Java application to convert liters to U.S. gallons. The program output is the number of U.S. gallons; the input is the number of liters. The computation requires knowing the conversionformula. A quick Google search shows 1L = 0.264 gallons. Therefore,

    x liters .264gallons

    = y gallonsliter

    The pseudo-code algorithm is straightforward:

    The coded Java program is shown below.

    123

    456789

    101112131415161718

    public class LitersToGallons{

    public static void main( String [] args )

    {// declare datafinal double GAL_PER_L = 0.264; // 1L = 0.264 U.S. gal.double liters; // amount in litersdouble gallons; // amount in U.S. gallons// input datajava.util.Scanner in = new java.util.Scanner( System.in );System.out.print( "How many liters? " );liters = in.nextDouble( );// calculate gallonsgallons = GAL_PER_L * liters;// output resultsSystem.out.println( liters + " L = " + gallons + " gal." );

    }}

    input liters

    gallons = 264 liters output gallons

  • 8/10/2019 01 Ipo Programming Examples

    2/4

    IPO Programming Examples Page 2

    Convert U.S. Gallons to LitersThis is the inverse conversion of the previous example. Taking the conversion formula from thatexample and dividing both sides of the equation by the conversion factor gives this formula:

    x liters = y gallons 1 liters

    .264 gallon

    The program is obtained by replacing lines 11 14 in application LitersToGallons by these:

    11121314

    System.out.print( "How many gallons? " );gallons = in.nextDouble( );// calculate litersliters = gallons / GAL_PER_L;

    Height versus TimePretend youre in a hot -air balloon. Write a Java application that calculates how far up you are infeet based on the number of seconds it takes for a dropped ball to reach the ground.

    The output is height in feet; the input is the number of seconds it takes the dropped ball to reachthe ground. Heres the first algorithm:

    A Google search of calculate height by timing falling object discovers this formula:

    height (meters) = 221 gt

    t is the time (in seconds) it takes the ball to reach the ground. g is the force of gravity (9.81meters per sec 2).

    The program is to calculate the height in feet, so the height in meters given by the above formulamust be converted using the formula:

    height (feet) = 3.281 height (meters)

    input the time t it takes the object to fall output height in feet

  • 8/10/2019 01 Ipo Programming Examples

    3/4

    IPO Programming Examples Page 3

    The pseudocode algorithm is:

    The coded Java program is shown below.

    public class Height{

    public static void main( String [] args ){

    // declare constantsfinal double G = 9.81; // gravity constantfinal double FT_PER_M = 3.281; // 1 m = 3.281 ft.// declare datadouble t; // number of seconds for falldouble h; // height// input datajava.util.Scanner in = new java.util.Scanner( System.in );

    System.out.print( "Enter time for ball to drop: " );t = in.nextDouble( );// calculate heighth = (G * t * t ) / 2.0; // height in metersh *= FT_PER_M; // in feet// print resultSystem.out.print( "Height = " + h + " ft." );

    }}

    input the time t it takes the object to fall calculate height in meters =

    22

    1 gt

    calculate height in feet = 3 281 height in meters output height in feet

  • 8/10/2019 01 Ipo Programming Examples

    4/4

    IPO Programming Examples Page 4

    Workers Gross PayWrite a Java application that reads a workers hourly wage and the number of hours he or sheworked and prints his or her pay.

    The workers pay is the number of hours worked times the hourly wage.

    The pseudo-code algorithm is straightforward:

    The coded Java program is shown below. Notice that you need only create one Scanner objectfrom which to read all the data.

    123456

    789

    1011121314151617181920

    public class Pay{

    public static void main( String [] args ){

    // declare datadouble wage; // worker's hourly wage

    double hours; // worker's hours worked double pay; // calculated pay// build a Scanner object to obtain inputjava.util.Scanner in = new java.util.Scanner( System.in );// prompt for and read worker's dataSystem.out.println( "Wage and hours?" );wage = in.nextDouble( );hours = in.nextDouble( );// calculate pay

    pay = hours * wage;// print resultSystem.out.print( "Pay = $" + pay );

    }}

    input hourly wage input hours worked pay = hourly wage hours worked output pay