0% found this document useful (0 votes)
76 views

Ipo Programming Examples: Convert Liters To U.S. Gallons

The document provides examples of Java programs that perform unit conversions and calculations using the IPO (Input, Processing, Output) model. It includes programs to: 1) Convert between liters and gallons 2) Calculate height from falling time using the formula for gravity 3) Calculate a worker's gross pay based on hourly wage and hours worked For each example it shows the IPO diagram and provides the coded Java program.

Uploaded by

Abdul Jabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Ipo Programming Examples: Convert Liters To U.S. Gallons

The document provides examples of Java programs that perform unit conversions and calculations using the IPO (Input, Processing, Output) model. It includes programs to: 1) Convert between liters and gallons 2) Calculate height from falling time using the formula for gravity 3) Calculate a worker's gross pay based on hourly wage and hours worked For each example it shows the IPO diagram and provides the coded Java program.

Uploaded by

Abdul Jabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

IPO PROGRAMMING EXAMPLES

Convert Liters to U.S. Gallons


Write a Java application to convert liters to U.S. gallons. A quick Google search shows
1L = 0.264 gallons. Here’s an IPO diagram of the program:

INPUT PROCESSING OUTPUT

gallons
Liters x liters × .264 = y gallons U.S. Gallons
liter

The coded Java program is shown below.

1 import java.util.Scanner;
2
3 public class LitersToGallons
4 {
5 public static void main( String [] args )
6 {
7 // declare data
8 double liters; // amount in liters
9 double gallons; // amount in U.S. gallons
10 // input data
11 Scanner in = new Scanner( System.in );
12 System.out.print( "How many liters? " );
13 liters = in.nextDouble( );
14 // 1L = 0.264 U.S. gal.
15 gallons = 0.264 * liters;
16 // output results
17 System.out.print( liters + " L = " );
18 System.out.println( gallons + " U.S. gal." );
19 }
20 }

IPO Programming Examples Page 1


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

1 liters
x liters = y gallons ×
.264 gallon

INPUT PROCESSING OUTPUT

1 liters
U.S. Gallons x liters = y gallons × Liters
.264 gallon

1 import java.util.Scanner;
2
3 public class GallonsToLiters
4 {
5 public static void main( String [] args )
6 {
7 // declare data
8 double liters; // amount in liters
9 double gallons; // amount in U.S. gallons
10 // input data
11 Scanner in = new Scanner( System.in );
12 System.out.print( "How many U.S. gallons? " );
13 gallons = in.nextDouble( );
14 // 1L = 0.264 U.S. gal.
15 liters = gallons / 0.264;
16 // output results
17 System.out.print( liters + " L = " );
18 System.out.println( gallons + " U.S. gal." );
19 }
20 }

IPO Programming Examples Page 2


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

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

height (meters) = ½gt2

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

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

height (feet) = 3.281 × height (meters)

Here’s the program’s input, processing and output:

INPUT PROCESSING OUTPUT

INTERNAL DATA
g = 9.81

time t (sec) height (meters) = ½gt2 height (m)


height (feet) = 3.281 × height (meters)

The coded Java program is shown on the next page. It uses the *= operator, which is covered in
the subsequent topic Assignment Operators.

IPO Programming Examples Page 3


1 import java.util.Scanner;
2
3 public class Height
4 {
5 public static void main( String [] args )
6 {
7 // declare data
8 double t; // number of seconds for fall
9 double h; // height
10 // input data
11 Scanner in = new Scanner( System.in );
12 System.out.print( "Enter time for ball to drop: " );
13 t = in.nextDouble( );
14 // calculate height
15 h = (9.81 * t * t ) / 2.0; // height in meters
16 h *= 3.281; // in feet, 1 m = 3.281 ft.
17 // print result
18 System.out.print( "Height = " + h + " ft." );
19 }
20 }

IPO Programming Examples Page 4


Worker’s Gross Pay
Write a Java application that reads a worker’s hourly wage and the number of hours he or she
worked and prints his or her pay. The worker’s pay is the number of hours worked times the
hourly wage.
INPUT PROCESSING OUTPUT

hourly wage
gross pay = hourly wage  hours worked gross pay
hours worked

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

1 import java.util.Scanner;
2
3 public class Pay
4 {
5 public static void main( String [] args )
6 {
7 // declare data
8 double wage; // worker's hourly wage
9 double hours; // worker's hours worked
10 double pay; // calculated pay
11 // build a Scanner object to obtain input
12 Scanner in = new Scanner( System.in );
13 // prompt for and read worker's data
14 System.out.println( "Wage and hours?" );
15 wage = in.nextDouble( );
16 hours = in.nextDouble( );
17 // calculate pay
18 pay = hours * wage;
19 // print result
20 System.out.print( "Pay = $" + pay );
21 }
22 }

IPO Programming Examples Page 5

You might also like