-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalary.java
More file actions
93 lines (80 loc) · 3.67 KB
/
Salary.java
File metadata and controls
93 lines (80 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Programmer: Dan Hopp
* Date: 20-NOV-2019
* Description:
• Create an array called salaryArray to store the hourly salary of 8
employees. Use the Math.random() method to randomly assign the hourly
salary to each employee. The salary should be in the range of $12.00 to
$75.00, inclusive.
• Print contents of the salaryArray.
• Change the hourly salary to $55.00 for the employee with ID 5.
• Print the salary and employee ID of the employee with the highest hourly
salary.
• Print the salary and employee ID of the employee with the lowest hourly
salary.
• Print the difference between the highest and lowest hourly salaries.
• Assume that the national average for an hourly salary is $22.00. Print
the number of employees with an hourly salary higher than the national
average.
*/
package lab8;
public class Salary {
public static void main(String[] args){
//Create an array called salaryArray to store the hourly salary of 8
//employees
double[] salaryArray = new double[8];
//randomly assign hourly salary to each employee
//$12.00 to $75.00, inclusive
for(int i = 0; i < salaryArray.length; i++){
salaryArray[i] = (Math.random() * 64) + 12;
}
//Print contents of the salaryArray
System.out.println("The contents of salaryArray are:");
for(int i = 0; i < salaryArray.length; i++){
System.out.printf("$%4.2f ", salaryArray[i]);
}
//blank line
System.out.println("\n");
//Change the hourly salary to $55.00 for the employee with ID 5
salaryArray[5] = 55.00;
//Print the salary and employee ID of the employee with the highest
//hourly salary
double highestSalary = salaryArray[0];
int highestSalaryID = 0;
for(int i = 1; i < salaryArray.length; i++){
if(salaryArray[i] > highestSalary){
highestSalary = salaryArray[i];
highestSalaryID = i;
}
}
System.out.printf("The highest salary is $%4.2f. The Employee ID is "
+ "%d.\n", highestSalary, highestSalaryID);
//Print the salary and employee ID of the employee with the lowest
//hourly salary
double lowestSalary = salaryArray[0];
int lowestSalaryID = 0;
for(int i = 1; i < salaryArray.length; i++){
if(salaryArray[i] < lowestSalary){
lowestSalary = salaryArray[i];
lowestSalaryID = i;
}
}
System.out.printf("The lowest salary is $%4.2f. The Employee ID is "
+ "%d.\n", lowestSalary, lowestSalaryID);
//Print the difference between the highest and lowest hourly salaries
System.out.printf("The difference between the highest and lowest " +
"hourly salaries is $%4.2f.\n", (highestSalary - lowestSalary));
//Assume that the national average for an hourly salary is $22.00. Print
//the number of employees with an hourly salary higher than the national
//average.
int higherThanAverage = 0;
for(int i = 0; i < salaryArray.length; i++){
if(salaryArray[i] > 22.00){
higherThanAverage++;
}
}
System.out.println("The number of employees with an hourly salary " +
"higher than the national average of $22.00 is " +
higherThanAverage + ".");
}
}