Ipo Programming Examples: Convert Liters To U.S. Gallons
Ipo Programming Examples: Convert Liters To U.S. Gallons
gallons
Liters x liters × .264 = y gallons U.S. Gallons
liter
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 }
1 liters
x liters = y gallons ×
.264 gallon
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 }
A Google search of calculate height by timing falling object discovers this formula:
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:
INTERNAL DATA
g = 9.81
The coded Java program is shown on the next page. It uses the *= operator, which is covered in
the subsequent topic Assignment Operators.
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 }