0

I'm new to JAVA and I struggled with this question which is very unclear to me. I'm politely asking if someone could discussing it with me or teach me how to answer it? Thank you.

Question:

A Company class holds an array of employees and its constructor takes in as parameters 4 arrays of the same length. The first array is an array of String being the names of employees; the second array is an array of String being the respective addresses; the third array is an array of int being the respective employee numbers; the fourth array is an array of double being the respective salaries.

I know how to do the public constructor one but I am unsure about the constructor of the class Company.

Your help is very valuable to me. Thank you in advance.

2
  • What are you unsure about? It just takes arrays as parameters. Commented Aug 9, 2017 at 19:34
  • why can't you do array of Employee Commented Aug 9, 2017 at 19:34

3 Answers 3

2

Feels like a very simple class structure.

public class Company {
Employee[] employees;

    public Company (String[] names, String[] addresses, int[] ids, double[] salaries) {
        employees = new Employee[names.length];
        for (int i = 0; i < employee.length; i++) {
            employees[i] = new Employee (names[i], addresses[i], ids[i], salaries[i]);
        }
    }

    static class Employee {
    String name;
    String address;
    int id;
    double salary;

        Employee (String name, String address, int id, double salary) {
            this.name = name;
            this.address = address;
            this.id = id;
            this.salary = salary;
        }
    }
}

Please delete question if this is a school assignment and you are using stackoverflow to cheat.

Sign up to request clarification or add additional context in comments.

Comments

1
public Company(String[] names, String[] addresses, int[] employeeNumbers, double[] salaries) {

}

A better design would be

public Company(Employee[] employees) {

}

and have an employee class

public class Employee {

    private String name;
    private String address;
    private int employeeNumber;
    private double salary; // you may not want to use this in production

    public Employee(String name, String address, int employeeNumber, double salary) {

    }
    // constructors, getters and setters, etc
}

According to @Kayaman comment seems you already have an employee class but for some reason you don't want to use the first constructor that takes an array of employees instead of 4 arrays. You want to use the first constructor with the 4 array parameters and construct array objects from the parameters. That's actually bad programming. It makes you code look dirty.

If it's a school assignment and you are required to do it that way, I'm guessing the instructor probably just wants you to learn something out of it. But code like that should not be written in production.

Anyways, here is the solution you might be looking for

public class Company {

    private Employee[] employees;

    public Company(String[] names, String[] addresses, int[] employeeNumbers, double[] salaries) {

        employees = new Employees[names.length]; // assuming all arrays are of the same length

        for (int i = 0;i < employees.length;i++) {
            employees[i] = new Employee(names[i], addresses[i], employeeNumbers[i], salaries[i]);
        }
    }
}

5 Comments

He does have an Employee class. He just edited the info about it out of the question.
I didn't ask a question.
Oh i c. I thought you comment started with 'How' instead of 'He' sorry
I believe he was suppose to use the first constructor in your answer, then create employee objects within it.
It was a bit clearer before he edited out information from it.
0
import java.util.ArrayList;
import java.util.Collections;    

public Class Company
{
  ArrayList<Employee> employees = new ArrayList<Employee>();

  public Company(Employee[] newEmployees)
  {
    employees.addAll(newEmployees);
  }
}

public Class Employee
{
  public String Name;
  public String Address;
  public Int Id;
  public Double Salary;

  public Employee(string name, string address, int id, double salary)
  {
     this.Name = name;
     this.Address = address;
     this.Salary = salary;
     this.Id = id;
  }
}

To create a new company:

Employee[] e = { 
  new Employee("Bob", "Bobs address", 1, 50000), 
  new Employee("Jane", "Janes address", 3, 85000) 
}

Company c = new Company(e);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.