Question

In: Computer Science

class Company uses an ArrayList of class Employee to manage its employees. Your job is to...

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:

  1. Constructor of the class :

    Employee(String firstName, String lastName){ }
  2. 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.

Solutions

Expert Solution

Here is the answer for your question in Java Programming Language.

Kindly upvote if you find the answer helpful.

################################################################

CODE :

Employee.java

class Employee {
// Declared employee variables
private static int uniqueId = 1001;
private String firstName;
private String lastName;
private int employeeId;
  
//Parameterized constructor

public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.employeeId = uniqueId;
uniqueId++;
}
//Accessor of the first name
public String getFirstName() {
return firstName;
}

@Override
public String toString() {
return firstName + " " + lastName + ", " + employeeId;
}
}

########################################################

Company.java


import java.util.ArrayList;


public class Company {
//Instance field
ArrayList<Employee> employees = new ArrayList<>();
  
//Default constructor
public Company() {
  
}
/** 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){
int rightIndex =0;
if(!employees.isEmpty())
rightIndex = (employees.size());
for(int i =0;i<employees.size();i++){
if(anEmployee.getFirstName().compareToIgnoreCase(employees.get(i).getFirstName()) < 0){
rightIndex = i;
break;
}
}
employees.add(rightIndex, anEmployee);
}

@Override
public String toString() {
String result = "[";
for(int i =0;i<employees.size();i++){
result += "[" + employees.get(i) + "]";
}
result += "]";
return result;
}
}

############################################################

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 :)


Related Solutions

Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each...
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each Clown only needs to be identified by a String for her/his name. Always join a new Clown to the end of the Parade. Only the Clown at the head of the Parade (the first one) can leave the Parade. Create a test application to demonstrate building a parade of 3 or 4 clowns (include your own name), then removing 1 or 2, then adding...
Java Coding Part A Create a class Employee. Employees have a name.   Also give Employee a...
Java Coding Part A Create a class Employee. Employees have a name.   Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for taxes (we will do a flat 17%), and the final amount (for...
Springfield Company offers a bonus plan to its employees and the amount of the employee bonuses...
Springfield Company offers a bonus plan to its employees and the amount of the employee bonuses for the current year is estimated to be $966,000 to be paid during January of the following year. The journal entry on December 31 to record the bonuses is: Debit Estimated Bonus Payable $966,000; credit Cash $966,000. Debit Employee Bonus Expense $966,000; credit Prepaid Employee Bonus $966,000. Debit Employee Bonus Expense $966,000; credit Bonus Payable $966,000. Debit Unearned Bonuses $966,000; credit Bonus Payable $966,000....
Kansas Company uses a job costing accounting system for its production costs. The company uses a...
Kansas Company uses a job costing accounting system for its production costs. The company uses a predetermined overhead rate based on direct labor-hours to apply overhead to individual jobs. The company prepared an estimate of overhead costs at different volumes for the current year as follows. Direct labor-hours 150,000 180,000 210,000 Variable overhead costs $ 1,200,000 $ 1,440,000 $ 1,680,000 Fixed overhead costs 612,000 612,000 612,000 Total overhead $ 1,812,000 $ 2,052,000 $ 2,292,000 The expected volume is 180,000 direct...
Kansas Company uses a job costing accounting system for its production costs. The company uses a...
Kansas Company uses a job costing accounting system for its production costs. The company uses a predetermined overhead rate based on direct labor-hours to apply overhead to individual jobs. The company prepared an estimate of overhead costs at different volumes for the current year as follows: Direct labor-hours 150,000 180,000 210,000 Variable overhead costs $ 1,050,000 $ 1,260,000 $ 1,470,000 Fixed overhead costs 684,000 684,000 684,000 Total overhead $ 1,734,000 $ 1,944,000 $ 2,154,000 The expected volume is 180,000 direct...
Kansas Company uses a job costing accounting system for its production costs. The company uses a...
Kansas Company uses a job costing accounting system for its production costs. The company uses a predetermined overhead rate based on direct labor-hours to apply overhead to individual jobs. The company prepared an estimate of overhead costs at different volumes for the current year as follows. Direct labor-hours 150,000 180,000 210,000 Variable overhead costs $ 900,000 $ 1,080,000 $ 1,260,000 Fixed overhead costs 630,000 630,000 630,000 Total overhead $ 1,530,000 $ 1,710,000 $ 1,890,000 The expected volume is 180,000 direct...
Kansas Company uses a job costing accounting system for its production costs. The company uses a...
Kansas Company uses a job costing accounting system for its production costs. The company uses a predetermined overhead rate based on direct labor-hours to apply overhead to individual jobs. The company prepared an estimate of overhead costs at different volumes for the current year as follows: Direct labor-hours 150,000 180,000 210,000 Variable overhead costs $ 900,000 $ 1,080,000 $ 1,260,000 Fixed overhead costs 702,000 702,000 702,000 Total overhead $ 1,602,000 $ 1,782,000 $ 1,962,000 The expected volume is 180,000 direct...
a Kansas Company uses a job costing accounting system for its production costs. The company uses...
a Kansas Company uses a job costing accounting system for its production costs. The company uses a predetermined overhead rate based on direct labor-hours to apply overhead to individual jobs. The company prepared an estimate of overhead costs at different volumes for the current year as follows: Direct labor-hours 150,000 180,000 210,000 Variable overhead costs $ 1,050,000 $ 1,260,000 $ 1,470,000 Fixed overhead costs 594,000 594,000 594,000 Total overhead $ 1,644,000 $ 1,854,000 $ 2,064,000 The expected volume is 180,000...
Your company asks you to manage a short-term guaranteed investment contract. Your job is to create...
Your company asks you to manage a short-term guaranteed investment contract. Your job is to create a portfolio that has a 4-year duration. (a) If you are given the choice of using a 5-year zero coupon bond and a 3-year 8% annual coupon bond with a yield to maturity of 10%, how would you construct your portfolio? (b) What is the modified duration of that 3-year annual coupon bond in (a)? (c) If the yield to maturity of that 3-year...
4. Suppose a company wants to find the determinants of job satisfaction for its employees. The...
4. Suppose a company wants to find the determinants of job satisfaction for its employees. The company gathers some anonymous data from its employees and runs a regression with Job Satisfaction (on a 1 to 10 scale) as the response variable, and it gets the following results. ANOVA df SS Regression 4 9283 Residual 14 370 (a) What are the null and alternative hypotheses (regarding the b values) for the F-test for the regression? (b) What is the F-statistic for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT