In: Computer Science
class Company uses an ArrayList of class Employee to manage its employees.
Your job is to create two classes , Company and Employee, with appropriate instance fields, constructors, and methods as been described in the set of questions that follows .
A sample use case of the classes is shown below:
public class CompanyTester{ public static void main(String[] args){ Company myCompany = new Company(); myCompany.add( new Employee("james","gasling")); myCompany.add( new Employee("bill","gate")); myCompany.add( new Employee("dennis","ritchie")); System.out.println(myCompany); } } The sample output of the program is shown below:
[[bill gate, 1002], [dennis ritchie, 1003], [james gasling, 1001]]
A) [5 marks] Create class Employee with following instance variables:
firstName (String), lastName (String), employeeId (int).
Each employee is assigned a unique number by the class Employee at construction time. The first employee number should be set to 1001.
Create following public methods for the class:
Constructor of the class :
Employee(String firstName, String lastName){ }
Accessor for the firstName
String getFirstname(){ }
Note: Just create the methods being asked. Do not add any extra methods.
B) [11 marks] Develop class Company that stores Employee objects in an ArrayList with following specifications:
[3 marks] Default constructor & instance fields
[8 marks] following public method
/** Takes an Employee object reference and adds it into the ArrayList to keep the Employee objects sorted by their first names.
@param anEmployee: an instance of an Employee abject
*/
public void addEmployee(Employee anEmployee)
Notes:
1. You should find the right index in the ArrayList and then add the employee object in the right index to keep the employees objects sorted based on their first names.
2. Your solution will not be marked if:
a. you add the employee object to the end of the arraylist and then sort it.
b. You use a temporary array or arraylist in this questions.
C) [4 marks] /** Override toString() methods of both Company and Employee classes that creates the same output format for the given sample data added to the company.(Refer to the same use case above)
Note: You should override the toString() methods to generate the same output format shown in test case above.
Here is the answer for your question in Java Programming Language.
Kindly upvote if you find the answer helpful.
################################################################
CODE :
Employee.java
class Employee { public Employee(String firstName, String lastName)
{ @Override |
########################################################
Company.java
@Override |
############################################################
CompanyTester.java
public class
CompanyTester{ public static void main(String[] args){ Company myCompany = new Company(); myCompany.addEmployee(new Employee("james","gasling")); myCompany.addEmployee(new Employee("bill","gate")); myCompany.addEmployee(new Employee("dennis","ritchie")); System.out.println(myCompany); } } |
######################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
Employee.java
Company.java
##################################################################
CompanyTester.java
################################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)