Using a while loop create a program that calculates the monthly salary of N employees[where N is the number of employees that's entered by the user]. If the employee worked less or equal to 160 hours, the employee will be paid N$240.00 per hour. For any hours greater than 160, the employee is paid N$320.00 per hour.
Sample Run1
Enter number of employees(N):3
Hours worked 1: 160
Hours worked 2: 200
Hours worked 3: 80
Output1:
Salary 1: N$38400.00
Salary 2: N$51200.00
Salary 3: N$19200.00
Sample Run 2
Enter number of employees(N):0
Output2:
No employees provided to do calculations
Sample Run 3
Enter number of employees(N):-4
Output 3:
Number of employees cannot be negative
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter number of employees(N): ");
int N = keyBoard.nextInt();
if (N > 0) {
int hoursWorked[] = new int[N];
int i = 0;
while (i < N) {
System.out.print("Hours worked " + (i + 1) + ": ");
hoursWorked[i] = keyBoard.nextInt();
i++;
}
i = 0;
while (i < N) {
// If the employee worked less or equal to 160 hours, the employee will be paid
// N$240.00 per hour.
double salary =0;
// For any hours greater than 160, the employee is paid N$320.00 per hour.
if (hoursWorked[i] <= 160) {
salary =hoursWorked[i] * 240.0;
}else {
salary =160 * 240.0+(hoursWorked[i]-160)*320.0;
}
System.out.printf("Salary %d: N$%.2f\n", (i + 1), salary);
i++;
}
} else if (N == 0) {
System.out.println("No employees provided to do calculations");
} else {
System.out.println("Number of employees cannot be negative");
}
keyBoard.close();
}
}
Comments
Leave a comment