Employee’s Weekly Wages - Java Sample Programs

/*The following program determines an employee’s weekly wages. If the hours worked
exceed 40, then wages include overtime payment.
*/
//Filename: WeeklyWages.java

import java.util.*;

public class WeeklyWages
{
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
*/

/* Explanation
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
into the variable rate. The statement in Line 8 checks whether the value of the
variable hours is greater than 40.0. If hours is greater than 40.0, then the wages
are calculated by the statement in Line 9, which includes overtime payment; otherwise,
the wages are calculated by the statement in Line 11. The statement in Line 12 outputs
the wages.

*/

No comments: