The first programming project involves writing a program that computes the salar
ID: 3741881 • 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. 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 n 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 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.
The google recommended Java style guide, provided as link in the week 2 content, should be used to
format and document your code. Specifically, the following style guide attributes should be addressed:
Header comments include filename, author, date and brief purpose of the program.
In-line comments used to describe major functionality of the code.
Meaningful variable names and prompts applied.
Class names are written in UpperCamelCase.
Variable names are written in lowerCamelCase.
Constant names are in written in All Capitals.
Braces use K&R style.
In addition the following design constraints should be followed:
Declare all instance variables private
Avoid the duplication of code
Test cases should be supplied in the form of table with columns indicating the input values, expected
output, actual output and if the test case passed or failed. This table should contain 4 columns with
appropriate labels and a row for each test case. Note that the actual output should be the actual results
you receive when running your program and applying the input for the test record. Be sure to select
enough different kinds of employees to completely test the program.
Note: All code should compile and run without issue.
Submission requirements
Deliverables include all Java files (.java) and a single word (or PDF) document. The Java files should be
named appropriately for your applications. The word (or PDF) document should include screen captures
showing the successful compiling and running of each of the test cases. Each screen capture should be
properly labeled clearly indicated what the screen capture represents. The test cases table should be
included in your word or PDF document and properly labeled as well.
Explanation / Answer
Code:-
import java.io.*;
class Employee{
protected String name;
protected double monthlySalary;
public Employee(String n,double sa){
name = n;
monthlySalary = sa;
}
public double annualSalary(){
return 12*monthlySalary;
}
public String toString(){
return "Name : "+name+" Monthly-Salary : "+monthlySalary;
}
}
class Salesman extends Employee{
private double annualSales;
public Salesman(String n,double sa,double as){
super(n,sa);
annualSales = as;
}
public double annualSalary(){
double result;
result = annualSales*0.02;
if(result >= 20000){
result = 20000;
}
return (12*monthlySalary)+result;
}
public String toString(){
return "Name : "+name+" Monthly-Salary : "+monthlySalary+" Annual-Sales : "+annualSales;
}
}
class Executive extends Employee{
private double stockPrice;
public Executive(String n,double sa,double sp){
super(n,sa);
stockPrice = sp;
}
public double annualSalary(){
double result;
if(stockPrice > 50){
result = 30000;
}
else
result = 0;
return (12*monthlySalary)+result;
}
public String toString(){
return "Name : "+name+" Monthly-Salary : "+monthlySalary+" Stock-Price : "+stockPrice;
}
}
class EmployeeCalc{
public static void main(String[] args) {
Employee[] e2014 = new Employee[10];
Employee[] e2015 = new Employee[10];
int count4 = 0,count5 = 0;
String name,type;
int year;
double sal;
double salestock = 0;
try{
File f = new File("start.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while(line != null){
String[] str = line.split(" ");
year = Integer.parseInt(str[0]);
name = str[2];
type = str[1];
sal = Double.parseDouble(str[3]);
if(!type.equals("Employee"))
salestock = Double.parseDouble(str[4]);
if(year == 2014){
if(type.equals("Employee")){
e2014[count4] = new Employee(name,sal);
count4++;
}
if(type.equals("Salesman")){
e2014[count4] = new Salesman(name,sal,salestock);
count4++;
}
if(type.equals("Executive")){
e2014[count4] = new Executive(name,sal,salestock);
count4++;
}
}
if(year == 2015){
if(type.equals("Employee")){
e2015[count5] = new Employee(name,sal);
count5++;
}
if(type.equals("Salesman")){
e2015[count5] = new Salesman(name,sal,salestock);
count5++;
}
if(type.equals("Executive")){
e2015[count5] = new Executive(name,sal,salestock);
count5++;
}
}
line = br.readLine();
}
}catch(Exception e){e.printStackTrace();}
System.out.println("FOR 2014 Year : ----------- : -------------- :");
for(int i = 0; i<count4;i++){
System.out.println(e2014[i].toString());
System.out.println("Annual Salary = "+e2014[i].annualSalary());
}
System.out.println("FOR 2015 Year : ----------- : -------------- :");
for(int i = 0; i<count5;i++){
System.out.println(e2015[i].toString());
System.out.println("Annual Salary = "+e2015[i].annualSalary());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.