JAVA: For each of the two years, 2014 and 2015, find the average of all salaries
ID: 3876925 • Letter: J
Question
JAVA: For each of the two years, 2014 and 2015, find the average of all salaries for all employees for that year and then display the averages from both years at the end of the program.
Driver.java
===========================
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Driver {
private static void display(String message, Object[] emp, int len) {
System.out.println(message);
for(int i = 0; i < len; i++) {
if(emp[i] instanceof Employee) {
System.out.println(emp[i] + " ");
System.out.println("------------------------ ");
}
else if(emp[i] instanceof Salesman) {
System.out.println(emp[i] + " ");
System.out.println("------------------------ ");
}
else if(emp[i] instanceof Executive) {
System.out.println(emp[i] + " ");
System.out.println("------------------------ ");
}
}
}
public static void main(String[] args) throws FileNotFoundException {
Object[] emp2014 = new Object[10];
Object[] emp2015 = new Object[10];
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);
String oneLine;
int i = 0, j = 0;
while (inFile.hasNextLine()) {
oneLine = inFile.nextLine();
String[] inputArr = oneLine.split("\s+");
Object emp = null;
if(inputArr[1].equals("Employee")) {
emp = new Employee(inputArr[2], Integer.parseInt(inputArr[3]));
}
else if(inputArr[1].equals("Salesman")) {
emp = new Salesman(inputArr[2], Integer.parseInt(inputArr[3]), Integer.parseInt(inputArr[4]));
}
else if(inputArr[1].equals("Executive")) {
emp = new Executive(inputArr[2], Integer.parseInt(inputArr[3]), Integer.parseInt(inputArr[4]));
}
else
System.out.println("Unknown Type of Employee: " + inputArr[2]);
if(emp != null) {
if(inputArr[0].equals("2014"))
emp2014[i++] = emp;
else if(inputArr[0].equals("2015"))
emp2015[j++] = emp;
}
}
inFile.close();
display("Employees of 2014", emp2014, i);
display("Employees of 2015", emp2015, j);
}
}
Employee.java
===================
public class Employee {
private String name;
private int monthlySalary;
public Employee(String name, int monthlySalary) {
this.name = name;
this.monthlySalary = monthlySalary;
}
public int getAnnualSalary() {
int totalPay = 0;
totalPay = 12 * monthlySalary;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The annual salary is: " + getAnnualSalary();
return str;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
}
Salesman.java
======================
public class Salesman extends Employee{
private int annualSales;
private int num = 0;
public Salesman(String name, int monthlySalary, int annualSales) {
super(name, monthlySalary);
this.annualSales = annualSales;
}
public int figureCommission() {
num = (int) (.02 * annualSales);
if(num >= 20000) {
num = 20000;
}
return num;
}
public int getAnnualSalary() {
num = (int) (.02 * annualSales);
if(num >= 20000) {
num = 20000;
}
int totalPay = (getMonthlySalary() * 12) + num;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The commission is: " + figureCommission()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The annual salary is: " + getAnnualSalary();
return str;
}
public int getCommission() {
return num;
}
public int getAnnualSales() {
return annualSales;
}
public void setAnnualSales(int annualSales) {
this.annualSales = annualSales;
}
public void setCommission(int num) {
this.num = num;
}
}
Executive.java
====================
public class Executive extends Employee{
private int stockPrice;
public Executive(String name, int monthlySalary, int stockPrice) {
super(name, monthlySalary);
this.stockPrice = stockPrice;
}
public int getAnnualSalary() {
int bonus = 0;
if(stockPrice > 50)
bonus = 30000;
int totalPay = (getMonthlySalary() * 12) + bonus;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The stock price is: " + getStockPrice()
+ " " + "The annual salary is: " + getAnnualSalary();
return str;
}
public int getStockPrice() {
return stockPrice;
}
public void setStockPrice(int stockPrice) {
this.stockPrice = stockPrice;
}
}
File.txt
=========================
2015 Employee Campbell,Steve 3000
2014 Salesman Sanchez,Carlos 4000 200000
2015 Executive Oduala,Barack 6000 60
Explanation / Answer
Hello There,
The average of all salaries for all employees for a year can be calculated by calculating the total salary of employees for that year on looping through the arrays emp2014 and emp2015 that were populated in the program and by finally dividing the total by the number of employees.
PFB code for the same along with the output of test run. The code added to the provided code was highlighted as bold.
Driver.java
===========================
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Driver {
private static void display(String message, Object[] emp, int len) {
System.out.println(message);
for(int i = 0; i < len; i++) {
if(emp[i] instanceof Employee) {
System.out.println(emp[i] + " ");
System.out.println("------------------------ ");
}
else if(emp[i] instanceof Salesman) {
System.out.println(emp[i] + " ");
System.out.println("------------------------ ");
}
else if(emp[i] instanceof Executive) {
System.out.println(emp[i] + " ");
System.out.println("------------------------ ");
}
}
}
public static void main(String[] args) throws FileNotFoundException {
Object[] emp2014 = new Object[10];
Object[] emp2015 = new Object[10];
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);
String oneLine;
int i = 0, j = 0;
while (inFile.hasNextLine()) {
oneLine = inFile.nextLine();
String[] inputArr = oneLine.split("\s+");
Object emp = null;
if(inputArr[1].equals("Employee")) {
emp = new Employee(inputArr[2], Integer.parseInt(inputArr[3]));
}
else if(inputArr[1].equals("Salesman")) {
emp = new Salesman(inputArr[2], Integer.parseInt(inputArr[3]), Integer.parseInt(inputArr[4]));
}
else if(inputArr[1].equals("Executive")) {
emp = new Executive(inputArr[2], Integer.parseInt(inputArr[3]), Integer.parseInt(inputArr[4]));
}
else
System.out.println("Unknown Type of Employee: " + inputArr[2]);
if(emp != null) {
if(inputArr[0].equals("2014"))
emp2014[i++] = emp;
else if(inputArr[0].equals("2015"))
emp2015[j++] = emp;
}
}
inFile.close();
display("Employees of 2014", emp2014, i);
display("Employees of 2015", emp2015, j);
int totalSalary = 0;
int numberOfEmployees = 0;
for (Object object : emp2014) {
if(object instanceof Employee) {
totalSalary += ((Employee) object).getAnnualSalary();
numberOfEmployees++;
}else if(object instanceof Executive) {
totalSalary += ((Executive) object).getAnnualSalary();
numberOfEmployees++;
}else if(object instanceof Salesman) {
totalSalary += ((Salesman) object).getAnnualSalary();
numberOfEmployees++;
}
}
System.out.println("Average Salary of 2014 : "+ totalSalary/numberOfEmployees);
totalSalary = 0;
numberOfEmployees = 0;
for (Object object : emp2015) {
if(object instanceof Employee) {
totalSalary += ((Employee) object).getAnnualSalary();
numberOfEmployees++;
}else if(object instanceof Executive) {
totalSalary += ((Executive) object).getAnnualSalary();
numberOfEmployees++;
}else if(object instanceof Salesman) {
totalSalary += ((Salesman) object).getAnnualSalary();
numberOfEmployees++;
}
}
System.out.println("Average Salary of 2015 : "+ totalSalary/numberOfEmployees);
}
}
Employee.java
===================
public class Employee {
private String name;
private int monthlySalary;
public Employee(String name, int monthlySalary) {
this.name = name;
this.monthlySalary = monthlySalary;
}
public int getAnnualSalary() {
int totalPay = 0;
totalPay = 12 * monthlySalary;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The annual salary is: " + getAnnualSalary();
return str;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
}
Salesman.java
======================
public class Salesman extends Employee{
private int annualSales;
private int num = 0;
public Salesman(String name, int monthlySalary, int annualSales) {
super(name, monthlySalary);
this.annualSales = annualSales;
}
public int figureCommission() {
num = (int) (.02 * annualSales);
if(num >= 20000) {
num = 20000;
}
return num;
}
public int getAnnualSalary() {
num = (int) (.02 * annualSales);
if(num >= 20000) {
num = 20000;
}
int totalPay = (getMonthlySalary() * 12) + num;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The commission is: " + figureCommission()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The annual salary is: " + getAnnualSalary();
return str;
}
public int getCommission() {
return num;
}
public int getAnnualSales() {
return annualSales;
}
public void setAnnualSales(int annualSales) {
this.annualSales = annualSales;
}
public void setCommission(int num) {
this.num = num;
}
}
Executive.java
====================
public class Executive extends Employee{
private int stockPrice;
public Executive(String name, int monthlySalary, int stockPrice) {
super(name, monthlySalary);
this.stockPrice = stockPrice;
}
public int getAnnualSalary() {
int bonus = 0;
if(stockPrice > 50)
bonus = 30000;
int totalPay = (getMonthlySalary() * 12) + bonus;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The stock price is: " + getStockPrice()
+ " " + "The annual salary is: " + getAnnualSalary();
return str;
}
public int getStockPrice() {
return stockPrice;
}
public void setStockPrice(int stockPrice) {
this.stockPrice = stockPrice;
}
}
File.txt
=========================
2015 Employee Campbell,Steve 3000
2014 Salesman Sanchez,Carlos 4000 200000
2015 Executive Oduala,Barack 6000 60
Output of test run:
=================================
Employees of 2014
The name of the employee is: Sanchez,Carlos
The commission is: 4000
The monthly salary is: 4000
The annual salary is: 52000
------------------------
Employees of 2015
The name of the employee is: Campbell,Steve
The monthly salary is: 3000
The annual salary is: 36000
------------------------
The name of the employee is: Oduala,Barack
The monthly salary is: 6000
The stock price is: 60
The annual salary is: 102000
------------------------
Average Salary of 2014 : 52000
Average Salary of 2015 : 6900
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.