Java Reference
In-Depth Information
EXAMPLE 4-12
The following program determines an employee's weekly wages. If the hours worked
exceed 40 , then wages include overtime payment.
//Weekly Wages
import java.util.*;
public class WeeklyWages
{
4
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
double wages, rate, hours;
//Line 1
System.out.print("Line 2: Enter the working "
+ "hours: ");
//Line 2
hours = console.nextDouble();
//Line 3
System.out.println();
//Line 4
System.out.print("Line 5: Enter the pay "
+ "rate: ");
//Line 5
rate = console.nextDouble();
//Line 6
System.out.println();
//Line 7
if (hours > 40.0)
//Line 8
wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);
//Line 9
else
//Line 10
wages = hours * rate;
//Line 11
System.out.printf("Line 12: The wages are $%.2f %n",
wages);
//Line 12
System.out.println();
//Line 13
}
}
Sample Run: (In this sample run, the user input is shaded.)
Line 2: Enter working hours: 60
Line 5: Enter pay rate: 10
Line 12: The wages are $700
The statement in Line 1 declares the appropriate variables. The statement in Line 2
prompts the user to input the number of hours worked. The statement in Line 3 inputs
and stores the working hours in the variable hours . The statement in Line 5 prompts
the user to input the pay rate. The statement in Line 6 inputs and stores the pay rate
Search WWH ::




Custom Search