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

The first programming project involves writing a program that computes the salar

ID: 2247245 • Letter: T

Question

The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. 1

1. The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods:

a. A constructor that allows the name and monthly salary to be initialized.

b. A method named annualSalary that returns the salary for a whole year.

c. A toString method that returns a string containing the name and monthly salary, appropriately labeled.

2. The Employee class has two subclasses. The first is Salesman. It has an additional instance variable that contains the annual sales in whole dollars for that salesman. It should have the same three methods:

a. A constructor that allows the name, monthly salary and annual sales to be initialized.

b. An overridden method annualSalary that returns the salary for a whole year. The salary for a salesman consists of the base salary computed from the monthly salary plus a commission. The commission is computed as 2% of that salesman's annual sales. The maximum commission a salesman can earn is $20,000.

c. An overridden toString method that returns a string containing the name, monthly salary and annual sales, appropriately labeled.

3. The second subclass is Executive. It has an additional instance variable that reflects the current stock price. It should have the same three methods:

a. A constructor that allows the name, monthly salary and stock price to be initialized.

b. An overridden method annualSalary that returns the salary for a whole year. The salary for an executive consists of the base salary computed from the monthly salary plus a bonus. The bonus is $30,000 if the current stock price is greater than $50 and nothing otherwise.

c. An overridden toString method that returns a string containing the name, monthly salary and stock price, appropriately labeled.

4. Finally there should be a fourth class that contains the main method. It should read in employee information from a text file. Each line of the text file will represent the information for one employee for one year. An example of how the text file will look is shown below:

2014 Employee Smith,John 2000

2015 Salesman Jones,Bill 3000 100000

2014 Executive Bush,George 5000 55

The year is the first data element on the line. The file will contain employee information for only two years: 2014 and 2015. Next is the type of the employee followed by the employee name and the monthly salary. For salesmen, the final value is their annual sales and for executives the stock price. As the employees are read in, Employee objects of the appropriate type should be created and they should be stored in one of two arrays depending upon the year. You may assume that the file will contain no more than ten employee records for each year and that the data in the file will be formatted correctly.

Once all the employee data is read in, a report should be displayed on the console for each of the two years. Each line of the report should contain all original data supplied for each employee together with 2 that employee's annual salary for the year. For each of the two years, an average of all salaries for all employees for that year should be computed and displayed.

Explanation / Answer

package Online;

import java.io.File;

import java.util.Scanner;

//Class Employee definition

class Employee

{

//Instance variables

String empName;

double monthlySalary;

//Constructor

Employee(String name, double salary)

{

empName = name;

monthlySalary = salary;

}//End of constructor

//Method to return annual salary

double getAnnualySalary()

{

//Calculates annual salary

return (monthlySalary * 12);

}//End of method

@Override

//Overrides to String method

public String toString()

{

return " Employee Name: " + empName + " Monthly Salary = " + monthlySalary;

}//End of method

}//End of class

//Class Salesman derived from Employee

class Salesman extends Employee

{

//Instance variable

double annualSales;

//Constructor

Salesman(String name, double salary, double sales)

{

//Calls base class constructor

super(name, salary);

annualSales = sales;

}//End of constructor

//Overrides base class method

double getAnnualSalary()

{

//Calculates commission

double sa = monthlySalary * 12.0 * 0.002;

//Checks sales commission cannot be more than 2000

if(sa > 20000)

sa = 20000;

return sa;

}//End of method

@Override

//Overrides to String method

public String toString()

{

//Calls the base class toString() method and concatenates annual sales

return super.toString() + " Annual Sales = " + annualSales;

}//End of method

}//End of class

//Class Executive derived from Employee

class Executive extends Employee

{

//Instance variable

double currentStockPrice;

//Constructor

Executive(String name, double salary, double stock)

{

//Calls base class constructor

super(name, salary);

currentStockPrice = stock;

}//End of constructor

//Overrides base class method

double getAnnualSalary()

{

//Checks if stock price is more than 50 then add 30000 to annual salary

if(currentStockPrice > 50)

return monthlySalary * 12.0 + 30000;

else

return monthlySalary * 12.0;

}//End of method

@Override

//Overrides to String method

public String toString()

{

//Calls base class toString() method and concatenates stock price

return super.toString() + " Stock Price = " + currentStockPrice;

}//End of method

}//End of class

//Driver class

public class EmployeeDriver

{

//Main method

public static void main(String ss[])

{

//Local variable to store file data

int year = 0;

String empType = "", name = "";

double salary = 0, sales = 0, stockPrice = 0;

String data = "";

//File object created

File file = new File("D:/MyProgram/src/Online/Data.txt");

//Creates array type objects for the year 2014 and 2015

Employee e2014[] = new Employee[10];

Employee e2015[] = new Employee[10];

//Counter for 2014 and 2015

int c2014 = 0, c2015 = 0;

//Handles exception

try

{

//Scanner object created

Scanner scanner = new Scanner(file);

//Checks for the data availability

while(scanner.hasNext())

{

//Reads a line of string from the file

data = scanner.nextLine();

//Split the data with space

String[] splited = data.split(" ");

//Stores the year after converting it to integer

year = Integer.parseInt(splited[0]);

//Stores the employee type

empType = splited[1];

//Stores the name

name = splited[2];

//Stores the salary after converting it to double

salary = Double.parseDouble(splited[3]);

//Checks the employee type if it is sales man then stores the sales after converting it to double

if(empType.equals("Salesman"))

sales = Double.parseDouble(splited[4]);

//Checks the employee type if it is executive then stores the sales after converting it to double

else if(empType.equals("Executive"))

stockPrice = Double.parseDouble(splited[4]);

//System.out.println(year + " " + empType + " " + name + " " + salary + " " + sales + " " + stockPrice);

//Checks the employee type and year and creates object for the appropriate class

//and calls the constructor

if(empType.equals("Employee") && year == 2014)

e2014[c2014++] = new Employee(name, salary);

else if(empType.equals("Employee") && year == 2015)

e2015[c2015++] = new Employee(name, salary);

else if(empType.equals("Salesman") && year == 2014)

e2014[c2014++] = new Salesman(name, salary, sales);

else if(empType.equals("Salesman") && year == 2015)

e2015[c2015++] = new Salesman(name, salary, sales);

else if(empType.equals("Executive") && year == 2014)

e2014[c2014++] = new Executive(name, salary, stockPrice);

else if(empType.equals("Executive") && year == 2015)

e2015[c2015++] = new Executive(name, salary, stockPrice);

}//End of while

}//End of try

//Catch block

catch(Exception e)

{

e.printStackTrace();

}//End of catch

//Local variable for total salary and average salary

double totSalary = 0.0, avgSalary = 0.0;

//Displays year wise information

System.out.println(" Year: 2014");

//Loops till the counter for the year 2014

for(int co = 0; co < c2014; co++)

{

System.out.println(e2014[co]);

System.out.println(" Annual Salary: " + e2014[co].getAnnualySalary());

//Calculates total salary

totSalary += e2014[co].getAnnualySalary();

}//End of loop

//Calculates average salary and displays it

System.out.println(" Average Salary: " + totSalary / c2014);

System.out.println(" Year: 2015");

//Loops till the counter for the year 2015

for(int co = 0; co < c2015; co++)

{

System.out.println(e2015[co]);

e2015[co].getAnnualySalary();

System.out.println(" Annual Salary: " + e2015[co].getAnnualySalary());

//Calculates total salary

totSalary += e2015[co].getAnnualySalary();

}//End of loop

//Calculates average salary and displays it

System.out.println(" Average Salary: " + totSalary / c2015);

}//End of main

}//End of class

File (Data.txt) Contents:

2014 Employee Smith,John 2000
2015 Salesman Jones,Bill 3000 100000
2014 Executive Bush,George 5000 55

Sample Run:

Year: 2014

Employee Name: Smith,John
Monthly Salary = 2000.0
Annual Salary: 24000.0

Employee Name: Bush,George
Monthly Salary = 5000.0
Stock Price = 55.0
Annual Salary: 60000.0

Average Salary: 42000.0

Year: 2015

Employee Name: Jones,Bill
Monthly Salary = 3000.0
Annual Sales = 100000.0
Annual Salary: 36000.0

Average Salary: 120000.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote