Below is my Main Class for an Employee Project. The code wont run and keeps poin
ID: 3853164 • Letter: B
Question
Below is my Main Class for an Employee Project. The code wont run and keeps pointing to the array as the error. Im confused as to why it wont run any results.
public class Employee {
//variables
private String name;
public int monthlySalary;
//Constructor
public Employee(String name, int salary){
this.name = name;
this.monthlySalary = salary;
}
//Annual Salary method
public int annualSalary(){
return monthlySalary * 12;
}
@Override
public String toString(){
String strEmployee;
strEmployee = "Name: " + name + " ";
strEmployee += "Monthly Salary: $" + monthlySalary + " ";
return strEmployee;
}
}
public class Executive extends Employee {
//Executive variables
int stockPrice;
//Executive method to initialize variables
public Executive(String name, int salary, int StockPrice) {
super(name, salary);
this.stockPrice = stockPrice;
}
//Annual Salary method to include calculation for bonus if applicable
@Override
public int annualSalary() {
int bonus = 3000;
if (stockPrice > 50) {
return super.annualSalary() + bonus;
}
else{
return super.annualSalary();
}
}
//Print out Executive StockPrice
@Override
public String toString() {
return super.toString()+ "Stock Price: %s " + stockPrice;
}
}
public class Salesman extends Employee {
//Salesman variables
int annualSales;
//Salesman constructor
public Salesman(String name, int salary, int annualSales) {
super(name, salary);
this.annualSales = annualSales;
}
//Annual salary method containing calculation for commission
@Override
public int annualSalary() {
double commission = 0;
int baseSalary = super.annualSalary();
commission = 0.02 * annualSales;
// maximum commission can be earned is 20000
if (commission > 20000) {
commission = 20000;
}
//return value of Salesman salary to include commission
return baseSalary + (int) commission;
}
@Override
public String toString() {
String toSalesman;
toSalesman = super.toString();
toSalesman += "Annual Sales: " + annualSales + " ";
return toSalesman;
}
}
/** * File name: Driver.java * Author: * Date: */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Driver { private static Scanner input; private static Employee[] employees2014; private static Employee[] employees2015; private static int numEmployees2014 = 0; private static int numEmployees2015 = 0; private static int averageSalaries2014; private static int averageSalaries2015; /** * calculates and returns the average salary of 2014 */ private static int calcAverage2014Salary() { int totalSalary = 0; int averageSalary; // calculate total salary for (int i = 0; i < numEmployees2014; i++) { totalSalary += employees2014[i].annualSalary(); } // calculate average salary averageSalary = totalSalary / numEmployees2014; return averageSalary; } /** * calculates and returns the average salary of 2015 */ private static int calcAverage2015Salary() { int totalSalary = 0; int averageSalary; // calculate total salary for (int i = 0; i < numEmployees2015; i++) { totalSalary += employees2015[i].annualSalary(); } // calculate average salary averageSalary = totalSalary / numEmployees2015; return averageSalary; } /** * display employees */ private static void displayEmployees() { System.out.println("Year 2014"); System.out.println("---------"); for (int i = 0; i < numEmployees2014; i++) { System.out.println(employees2014[i]); System.out.println("Annual Salary: " + employees2014[i].annualSalary() + " "); } System.out.println("Average Salary: " + averageSalaries2014 + " "); System.out.println(""); System.out.println(" Year 2015"); System.out.println("---------"); for (int i = 0; i < numEmployees2015; i++) { System.out.println(employees2015[i]); System.out.println("Annual Salary: " + employees2015[i].annualSalary() + " "); System.out.println(""); } System.out.println("Average Salary: " + averageSalaries2015 + " "); System.out.println(""); } /* * main method **/ public static void main(String[] args) { String[] lineParts; int monthlySalary; int annualSales; int stockPrice; String year; String className; String name; // initialize the arrays, there would be no more than 10 Employees total employees2014 = new Employee[5]; employees2015 = new Employee[5]; // open input file to read File file = new File("/Users/TMK/Documents/text.txt"); // scanner object to read file try { input = new Scanner(file); while (input.hasNextLine()) { String line = input.nextLine(); // split line by a space lineParts = line.split(" "); Employee objEmployee; // get data from line partss year = lineParts[0]; className = lineParts[1]; name = lineParts[2]; monthlySalary = Integer.parseInt(lineParts[3]); // create object as per the Employee type // if Salesman if (className.equalsIgnoreCase("Salesman")) { annualSales = 0; annualSales = Integer.parseInt(lineParts[4]); objEmployee = new Salesman(name, monthlySalary, annualSales); } // if Executive else if (className.equalsIgnoreCase("Executive")) { stockPrice = Integer.parseInt(lineParts[5]); objEmployee = new Executive(name, monthlySalary, stockPrice); } else { objEmployee = new Employee(name, monthlySalary); } // if year is 2014 if (year.equals("2014")) { employees2014[numEmployees2014] = objEmployee; numEmployees2014++; } else if (year.equals("2015")) { employees2015[numEmployees2015] = objEmployee; numEmployees2015++; } } input.close(); // calculate average salaries averageSalaries2014 = calcAverage2014Salary(); averageSalaries2015 = calcAverage2015Salary(); // display all employees displayEmployees(); } catch (FileNotFoundException e) { System.out.println("Exception: " + e.getMessage()); } } }
Explanation / Answer
The problem was in the following line
stockPrice = Integer.parseInt(lineParts[5]);
you are trying to access index 5 which does not exist. From your code, I understand that an executive line in file will have something like
2014 executive Peter 250 108
i.e. 5 values in a line and the last value is the stock price. The 5th value should be accessed using index 4 . So when I changed the index value to 4 and ran it on my test file, the program ran with out any errors.
Below is the fixed code with sample input and output. Post a comment if it does not work still for you . In that case, I will need your input file contents to fix the issue. So you will need to edit the question and paste the input file contents.
If the answer worked for you, please don't forget to rate it. Thank you.
Driver.java
/** * File name: Driver.java * Author: * Date: */
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Driver
{
private static Scanner input;
private static Employee[] employees2014;
private static Employee[] employees2015;
private static int numEmployees2014 = 0;
private static int numEmployees2015 = 0;
private static int averageSalaries2014;
private static int averageSalaries2015;
/** * calculates and returns the average salary of 2014 */
private static int calcAverage2014Salary()
{
int totalSalary = 0;
int averageSalary;
// calculate total salary
for (int i = 0; i < numEmployees2014; i++)
{
totalSalary += employees2014[i].annualSalary();
}
// calculate average salary
averageSalary = totalSalary / numEmployees2014;
return averageSalary;
}
/** * calculates and returns the average salary of 2015 */
private static int calcAverage2015Salary()
{
int totalSalary = 0;
int averageSalary;
// calculate total salary
for (int i = 0; i < numEmployees2015; i++)
{
totalSalary += employees2015[i].annualSalary();
}
// calculate average salary
averageSalary = totalSalary / numEmployees2015;
return averageSalary;
}
/** * display employees */
private static void displayEmployees()
{
System.out.println("Year 2014");
System.out.println("---------");
for (int i = 0; i < numEmployees2014; i++)
{
System.out.println(employees2014[i]);
System.out.println("Annual Salary: " + employees2014[i].annualSalary() + " ");
}
System.out.println("Average Salary: " + averageSalaries2014 + " ");
System.out.println("");
System.out.println(" Year 2015");
System.out.println("---------");
for (int i = 0; i < numEmployees2015; i++)
{
System.out.println(employees2015[i]);
System.out.println("Annual Salary: " + employees2015[i].annualSalary() + " ");
System.out.println("");
}
System.out.println("Average Salary: " + averageSalaries2015 + " ");
System.out.println("");
}
/* * main method **/
public static void main(String[] args)
{
String[] lineParts;
int monthlySalary;
int annualSales;
int stockPrice;
String year;
String className;
String name;
// initialize the arrays, there would be no more than 10 Employees total
employees2014 = new Employee[5];
employees2015 = new Employee[5];
// open input file to read
File file = new File("/Users/TMK/Documents/text.txt");
//File file = new File("employee.txt");
// scanner object to read file
try {
input = new Scanner(file);
while (input.hasNextLine())
{
String line = input.nextLine();
// split line by a space
lineParts = line.split(" ");
Employee objEmployee;
// get data from line partss
year = lineParts[0];
className = lineParts[1];
name = lineParts[2];
monthlySalary = Integer.parseInt(lineParts[3]);
// create object as per the Employee type
// if Salesman
if (className.equalsIgnoreCase("Salesman"))
{
annualSales = 0;
annualSales = Integer.parseInt(lineParts[4]);
objEmployee = new Salesman(name, monthlySalary, annualSales);
}
// if Executive
else if (className.equalsIgnoreCase("Executive"))
{
stockPrice = Integer.parseInt(lineParts[4]);
objEmployee = new Executive(name, monthlySalary, stockPrice);
}
else
{
objEmployee = new Employee(name, monthlySalary);
}
// if year is 2014
if (year.equals("2014"))
{
employees2014[numEmployees2014] = objEmployee;
numEmployees2014++;
}
else if (year.equals("2015"))
{
employees2015[numEmployees2015] = objEmployee;
numEmployees2015++;
}
}
input.close();
// calculate average salaries
averageSalaries2014 = calcAverage2014Salary();
averageSalaries2015 = calcAverage2015Salary();
// display all employees
displayEmployees();
}
catch (FileNotFoundException e)
{
System.out.println("Exception: " + e.getMessage());
}
}
}
input file : employee.txt
2014 salesman John 150 1200
2014 executive Peter 250 108
2014 salesman Henry 250 1000
2014 executive Bob 290 109
2014 employee Alice 120
2015 salesman John 250 1300
2015 executive Peter 350 120
output
Year 2014
---------
Name: John
Monthly Salary: $150
Annual Sales: 1200
Annual Salary: 1824
Name: Peter
Monthly Salary: $250
Stock Price: %s 0
Annual Salary: 3000
Name: Henry
Monthly Salary: $250
Annual Sales: 1000
Annual Salary: 3020
Name: Bob
Monthly Salary: $290
Stock Price: %s 0
Annual Salary: 3480
Name: Alice
Monthly Salary: $120
Annual Salary: 1440
Average Salary: 2552
Year 2015
---------
Name: John
Monthly Salary: $250
Annual Sales: 1300
Annual Salary: 3026
Name: Peter
Monthly Salary: $350
Stock Price: %s 0
Annual Salary: 4200
Average Salary: 3613
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.