Use only the instance variable absolutely necessary Modify the constructors acco
ID: 3573064 • Letter: U
Question
Use only the instance variable absolutely necessary
Modify the constructors accordingly
Look at the addEmployee method. Make the appropriate changes, simplifying the method as much as possible.
Look at the findEmployee method and make the appropriate changes. Simplify the method as much as possible.
The toString method can be simplified a lot. Go out to Java's API specification site if necessary and look at ArrayList's available methods. See if you can find something to use.
Test your new Company class with CompanyDriver.java - but make NO changes to the driver.
Your output should contain all the same information, just printed in a different format.
----------------------------------DRIVER:
Explanation / Answer
Your program is perfectly alright. Please find the program with small modifications:
There is no way to get all the elements from arraylist without looping it. Thank you!
public class Company {
private final int DEFAULT_SIZE = 10;
private Employee[] personnel; // array to hold Employees
private int size; // size of the array, i.e. max number of
// employees that company can have
private int count; // actual number of Employees in array
// default constructor - builds a company that can have a max of 10
// employees but has zero to start
public Company() {
this.size = DEFAULT_SIZE;
personnel = new Employee[this.size];
count = 0;
}
// constructor - builds a company that can have a max of size-many
// employees but has zero to start
public Company(int size) {
this.size = size;
personnel = new Employee[size];
count = 0;
}
// addEmployee - if there is room in the company, adds an employee
// to the company and returns a 0. If there is no room left in the
// company, a -1 is returned
public int addEmployee(Employee e) {
if (count >= size) // Company is full, can't add employee
return -1;
personnel[count] = e;//new Employee(e); // Why are we creating a new object?
count++;
return 0;
}
// findEmployee - given a name as an argument, this method searches
// the company to find the name. If the employee is found, a new
// Employee object of that employee is created and returned, otherwise
// null is returned
public Employee findEmployee(String name) {
int i;
for (i = 0; i < count; i++) {
if (personnel[i].getName().equals(name)) {
return personnel[i];
}
}
return null;
}
// This is an incorrect version of findEmployee. If it finds the
// employee, it does NOT create a new object of the employee that
// it returns. The program CompanyDriver.java will demonstrate how
// this can lead to a programmer being able to change private data
public Employee findEmployee(int id) {
int i;
for (i = 0; i < count; i++) {
if (personnel[i].getId() == id) {
return personnel[i];
}
}
return null;
}
// toString - returns a string that is a list of all the employees
// in the company in the format:
// id:name, position, $xx,xxx.xx, MMM DD, YYYY
// id:name, position, $xx,xxx.xx, MMM DD, YYYY
// ...
public String toString() {
int i;
String str = "";
for (Employee e:personnel)
str += e.toString() + " ";
return str;
}
} // end class Company
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.