Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: How can I find the average yearly salary of all employees for each year(20

ID: 3876754 • Letter: J

Question

JAVA: How can I find the average yearly salary of all employees for each year(2014 and 2015)? I started to solve it but am stuck on a NullPointerException.

Driver.java

==================

package project1;

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("------------------------ ");

}

}

}

private static void average2014(Object[] emp2014) {

Object[][] array2014 = new Object[10][5];

String>

for(int i = 0; i < emp2014.length; i++) {

oneString = emp2014[i++].toString();

String[] lines = oneString.split("\s+");

for(int j = 0; j < lines.length; j++) {

array2014[i] = lines[i].split(",");

}

}

}

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();

average2014(emp2014);

display("Employees of 2014", emp2014, i);

display("Employees of 2015", emp2015, j);

}

}

Employee.java

====================

package project1;

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

====================

package project1;

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

===================

package project1;

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;

}

}

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;


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;
}
}

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;
}
}


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;
}
}


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("------------------------ ");
}
}
}
private static void average2014(Object[] emp2014) {
Object[][] array2014 = new Object[10][5];
String> for(int i = 0; i < emp2014.length; i++) {
> String[] lines = oneString.split("\s+");
for(int j = 0; j < lines.length; j++) {
array2014[i] = lines[i].split(",");
}
}
}
public static void main(String[] args) throws FileNotFoundException {
Object[] emp2014 = new Object[10];
Object[] emp2015 = new Object[10];

Employee []em2014= new Employee[10];//genarating employee objects
Employee []em2015= new Employee[10];
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);
String oneLine;
int i = 0,ii=0, j = 0,jj=0;
while (inFile.hasNextLine()) {
> String[] inputArr = oneLine.split("\s+");
Object emp = null;
Employee emr=null;
if(inputArr[1].equals("Employee")) {
//modified.........
emr = new Employee(inputArr[2], Integer.parseInt(inputArr[3]));
emp=emr;
if(inputArr[0].equals("2014"))//adding to array
em2014[ii++]=emr;
else em2015[jj++]=emr;

}
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();
average2014(emp2014);

//finding average salaries of all employes in 2014
double average2014=0;
int k=i;
for(i=0;i<ii;i++)
average2014=average2014+em2014[i].getAnnualSalary();//finding sum
//then average
average2014=average2014/ii;


//finding average salaries of all employes in 2015
double average2015=0;

for(i=0;i<jj;i++)
average2015=average2014+em2015[i].getAnnualSalary();//finding sum
//then average
average2015=average2015/jj;

i=k;
display("Employees of 2014", emp2014, i);
display("Employees of 2015", emp2015, j);
}
}