Question

In: Computer Science

***In Java language***The first programming project involves writing a program that computes the salaries for a...

***In Java language***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 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. 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.

Solutions

Expert Solution

Given below are the classes needed for the question. Hope it helps. If it does , please don't forget to rate the answer. Thank you very much.

Sol:

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 "\n Employee Name: " + empName + "\n 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() + "\n 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() + "\n 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("\n 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("\n Average Salary: " + totSalary / c2014);

System.out.println("\n 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("\n 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


Related Solutions

**C++ Programming Language** a program that computes the average of a collection of numbers and then...
**C++ Programming Language** a program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. The objective of this assignment is to select a language that you have never used before and modify the program from the text so that it outputs the number of A's, B's, C's, D's and F's. An A grade is any score that is at least 20% greater than the average. The...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain...
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain some methods. These will be regular methods (not static methods), so the class will have to be instantiated in order to use them. The class containing the main method will be provided and you will write the required methods and run the supplied class in order to test those methods. ? Specifications There are two separate files in this program. The class containing the...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock , for example 23:47:55). Here are the input errors (Exceptions)...
This project involves writing a program to calculate the terms of the following sequence of numbers:...
This project involves writing a program to calculate the terms of the following sequence of numbers: 0 1 1 3 5 11 21 43 … where each term is twice the second previous term plus the previous term. The 0th term of the sequence is 0 and the 1st term of the sequence is 1. The example below shows how to calculate the next sequence term: Current sequence: 0 1 Calculate next term: 2 * 0 + 1 = 1...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial...
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial of a specified number, or estimates the value of 'e' using the specified number of iterations (of a Taylor series). Please feel free to use the Internet to find resources that explain how to estimate the value of 'e' using a Taylor series. In the case of no or invalid number of parameters, the program should show help instructions that looks like: --- Assign...
The first assignment involves writing a Python program to compute the weekly pay for a salesman....
The first assignment involves writing a Python program to compute the weekly pay for a salesman. Your program should prompt the user for the number of hours worked for that week and the weekly sales. Your program should compute the total pay as the sum of the pay based on the number of hours worked times the hourly rate plus the commission. You should choose a value for the hourly pay. The commission should be computed as a percentage of...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT