(JAVA) Refer to the code from the files Employee.java, ClericalWorker.java, Prog
ID: 3768231 • Letter: #
Question
(JAVA)
Refer to the code from the files Employee.java, ClericalWorker.java, Programmer.java, and WebDeveloper.java posted in the following link: http://pastie.org/private/2p7ofcyob3wwdgazzi0btg
For the sake of reducing clutter in the prompt of this question I have used pastie to paste the code from all of the necessary files for the assignment. Each file is separated into its own respective section denoted by ##. I figured this would make things simpler and more clear. Using the files provided:
1. Modify the Programmer class so that the technologies array data member is an ArrayList of String. Also, make addTechnology just a bit more sophisticated: if we try to add Technology ‘tech’ to a Programmer that already has ‘tech’ in its array list, then no changes should be made to the array list. (I.e., the addTechnology operation does nothing in that case.) Furthermore, modify the getSalary method so that each Programmer who knows Java gets an extra $3000 bonus. Thus, for example, knowing C++ earns a $5000 bonus, but knowing Java earns $8000.
2. In the Employee class, change the access modifiers on getBaseSalary and setBaseSalary from private to protected.
3. Create a new file, ContractProgrammer.java, with a public class called ContractProgrammer that extends Programmer. (I.e. Every ContractProgrammer is-a Programmer.) Write a constructor for ContractProgrammer that takes two arguments: a name and a social security numbers. The job description of a contract programmer is “Contract programmer”. The base salary of a contract programmer is $70,000. In your constructor, use the setBaseSalary method from Part 3 to implement this. The salary of a contract programmer is simply his/her base salary: Contract programmers do not receive a bonus for learning technologies. Override the getSalary method in order to make this happen. You may call the getBaseSalary method.
4. Refer to the file Employees.txt. As you can see, each line in the file lists an employee, including the job description (i.e. type of employee), name, social security number, and technologies. The different fields are separated by colons.
5. Write a program called ReadEmployees.java with a main method that reads in each line (using the nextLine method), splits each input line into an array of strings (using the split method from Chapter 10), creates the appropriate employee objects (Employee, ClericalWorker, Programmer, WebDeveloper, ContractProgrammer) in accordance with the job descriptions, and stores them in an ArrayList. The program should then write a report to an output file called EmployeeReport.txt. The report should list information on each employee. Accomplish this by calling the toString method on each Employee object. Next, the report should print out the following summary information: (1) the number of employees, (2) the total salary of the employees, and (3) the average salary of the employees. The above should be done for each of the following categories of employee: (i) all employees, (ii) all programmers, (iii) all programmers who know Java, (iv) all employees who know Ocaml. Solve Parts iii and iv above by writing a public knowsTechnology method in the Programmer class that tests whether the programmer knows a particular technology. For instance, if Programmer p knows "Java" then the call, p.knowsTechnology("Java") should return true. The program should not attempt to print out an average salary if the number of employees in that category is zero.
6. Test your completed program. The contents of your EmployeeReport.txt file should match the following:
Employee Emil Post with SSN 111-11-1111 has salary $35, 000.00 Clerical worker Clara Barton with SSN 222-22-2222 has salary $45,000.00 Programmer Grace Murray Hopper with SSN 333-33-3333 has salary $80,000.00 and knows Assembly, Fortran, and Cobol Programmer Donald Ervin Knuth with SSN 444-44-4444 has salary $75,000.00 and knows Assembly and C Programmer William Nelson Joy with SSN 555-55-5555 has salary $88,000.00 and knows Java, C, csh, and Common Lisp Web developer Sir Timothy John Berners-Lee with SSN 666-66-6666 has salary $85,000.00 and knows HTML, JavaScript, Php, and ActionScript Contract programmer Richard Matthew Stallman with SSN 777-77-7777 has salary $70,000.00 and knows Scheme and Bash Contract programmer Shafrira Goldwasser with SSN 888-88-8888 has salary $70,000.00 and knows SML, Prolog, Java, and Haskell Number of employees:8 Total of employee salaries: $548,000.00 Average salary of employees: $68,500.00 Number of programmers: 6 Total of programmer salaries: $468,000.00 Average salary of programmers: $78,000.00 Number of Java programmers: 2 Total of Java programmer salaries: $158,000.00 Average salary of Java programmers: $79,000.00 Number of Ocaml programmers: 0 Total of Ocaml programmer salaries: $0.00Explanation / Answer
import java.util.*; public class Programmer extends Employee { ArrayList technologies = new ArrayList(); public Programmer(String name, String ssn) { super(name, ssn, 65000.00); } public void addTechnology(String tech) { if (technologies.contains(tech)) System.out.println("The technology is already contained in the array"); else technologies.add(tech); // } public double getSalary() { double salary = super.getSalary() + technologies.size() * 5000.00; if(technologies.contains("Java")){ salary = salary + 3000; return salary; } else return salary; } /*public String toString() { String returnVal = "Programmer " + super.toString() + " and knows"; for (String tech : technologies) { returnVal += " " + tech; // Note: Inefficient due to String concatenation. // Also lacks punctuation. } return returnVal; }*/ public String toString() // This version inserts commas between the technologies // It also generates the string efficiently, using a StringBuilder object. { StringBuilder returnVal = new StringBuilder("Programmer "); returnVal.append(super.toString()); if (technologies.size() > 0) { returnVal.append(" and knows "); for(String technology:technologies){ returnVal.append(technology).append(","); } } return returnVal.substring(0, returnVal.lastIndexOf(",")).toString(); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.