A university posts its faculty members’ salaries at http://cs.armstrong.edu/lian
ID: 3891757 • Letter: A
Question
A university posts its faculty members’ salaries at
http://cs.armstrong.edu/liang/data/Salary.txt. Each line in the file consists of a faculty
member’s first name, last name, rank, and salary, separated by a space. The first 10 lines
of data are as follows:
FirstName1 LastName1 assistant 79174.73
FirstName2 LastName2 associate 70817.75
FirstName3 LastName3 associate 69619.0
FirstName4 LastName4 full 116992.43
FirstName5 LastName5 full 116761.76
FirstName6 LastName6 full 123743.86
FirstName7 LastName7 assistant 70071.81
FirstName8 LastName8 assistant 67605.92
FirstName9 LastName9 associate 65534.42
FirstName10 LastName10 full 88528.43
Write a program to display the total salary for assistant professors, associate professors,
full professors, and all professors, respectively, and display the average salary for
assistant professors, associate professors, full professors, and all professors, respectively.
Here is a sample run. You output should look exactly as follows:
Data were retrieved from http://cs.armstrong.edu/liang/data/Salary.txt
Total salary for assistant professors is $20,246,511.91
Total salary for associate professors is $28,844,146.58
Total salary for full professors is $35,678,051.41
Total salary for all professors is $84,768,709.90
Average salary for assistant professors is $65,949.55
Average salary for associate professors is $83,849.26
Average salary for full professors is $102,229.37
Average salary for all professors is $84,768.71
Explanation / Answer
public class SalaryCalculator {
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(new File("Salary.txt"));
double tAssist = 0, tAssoc = 0, tFull = 0;
double aAssist = 0, aAssoc = 0, aFull = 0;
while (in.hasNextLine()) {
String line = in.nextLine();
String[] parts = line.split(" ");
switch (parts[2]) {
case "assistant":
aAssist++;
tAssist += Double.parseDouble(parts[3]);
break;
case "associate":
aAssoc++;
tAssoc += Double.parseDouble(parts[3]);
break;
case "full":
aFull++;
tFull += Double.parseDouble(parts[3]);
break;
}
}
System.out.println("Total Income for Assistant Professors is " + String.format("$%.2f", tAssist));
System.out.println("Total Income for Associate Professors is " + String.format("$%.2f", tAssoc));
System.out.println("Total Income for Full Professors is " + String.format("$%.2f", tFull));
System.out.println("Total Income for all professors is " + String.format("$%.2f", tAssist + tAssoc + tFull));
System.out.println("Average Income for Assistant Professors is " + String.format("$%.2f", tAssist / aAssist));
System.out.println("Average Income for Associate Professors is " + String.format("$%.2f", tAssoc / aAssoc));
System.out.println("Average Income for Full Professors is " + String.format("$%.2f", tFull / aFull));
System.out.println("Average Income for all professors is "
+ String.format("$%.2f", (tAssist + tAssoc + tFull) / (aAssist + aAssoc + aFull)));
in.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.