📄 practical quiz 6.employeearray.java
字号:
/**
* This class contains methods to process arrays of {@link Employee} objects.
*
* @author Neil
* @version 1.0.0
* @see Employee
*/
public class EmployeeArray {
/**
* Creates an array with three objects of class {@link Employee}.
* <p>
* The first element of the array is the object <code>first</code>, the
* second element of the array is the object <code>second</code>, and the
* third element of the array is the object <code>third</code>.
* </p>
*
* @param first
* a {@link Employee} object.
* @param second
* a {@link Employee} object.
* @param third
* a {@link Employee} object.
* @return an array with the objects <code>first</code>,
* <code>second</code>, and <code>third</code>.
*/
public static Employee[] makeArray(Employee first, Employee second,
Employee third) {
return new Employee[] { first, second, third };
}
/**
* Creates a new array from the specified array of {@link Employee} objects.
* <p>
* The elements in the new array have the same order as those in the
* specified array.
* </p>
*
* @param array
* an array that contains objects of class {@link Employee}.
* @return a <i>new</i> array of the objects in the specified array.
*/
public static Employee[] copyArray(Employee[] array) {
Employee[] temp = new Employee[array.length];
for (int i = 0; i < array.length; i++)
temp[i] = array[i];
return temp;
}
/**
* Returns the {@link Employee} object with the specified ID.
*
* @param array
* an array that contains objects of class {@link Employee}.
* @param id
* an employee ID.
* @return The {@link Employee} object with the specifed ID. Returns
* <code>null</code> if there are no employees in the specified
* array with the specifed ID.
*/
public static Employee getEmployee(Employee[] array, int id) {
if (array.length == 0)
return null;
for (Employee e : array) {
if (e.getId() == id)
return e;
}
return null;
}
/**
* Returns the number of employees whose salary is higher than the specified
* dollar amount.
*
* @param array
* an array that contains objects of class {@link Employee}.
* @param amount
* a dollar amount.
* @return the number of employees whose salary is higher than the specified
* dollar amount.
*/
public static int countHigherSalaries(Employee[] array, double amount) {
if (array.length == 0)
return 0;
int count = 0;
for (Employee e : array) {
if (e.getSalary() > amount)
count++;
}
return count;
}
/**
* Returns the sum of the salaries of the employees in the specified array.
*
* @param array
* an array that contains objects of class {@link Employee}.
* @return the sum of the salaries of the employees in the specified array.
*/
public static double sumSalaries(Employee[] array) {
if (array.length == 0)
return 0.0;
double result = 0;
for (Employee e : array) {
result += e.getSalary();
}
return result;
}
/**
* Obtains the highest salary in the specified array.
*
* @param array
* an array that contains objects of class {@link Employee}.
* @return the highest salary in the specified array.
*/
public static double getHighestSalary(Employee[] array) {
double result = array[0].getSalary();
for (Employee e : array) {
if (e.getSalary() > result)
result = e.getSalary();
}
return result;
}
/**
* Increases the salary of every employee in the specified array by the
* specified amount.
*
* @param array
* an array that contains objects of class {@link Employee}.
*/
public static void increaseAll(Employee[] array, double amount) {
if (array.length == 0)
return;
for (Employee e : array) {
e.setSalary(e.getSalary() + amount);
}
}
/**
* Returns a string representation of the specified {@link Employee} array.
* <p>
* Uses the method <code>toString</code> in class <code>Employee</code>
* to obtain the string representation of each object in the array.
* </p>
* A new line character ( \n ) separates the string representations of each
* <code>Employee</code> object. For example:
* </p>
*
* <pre>
* Employee[102,Mary Jones,68250.0]\n
* Employee[101,Joe Smith,36000.0]\n
* Employee[103,Richard Wong,92175.0]
* </pre>
*
* <p>
* Note that the string returned does <i>not</i> end with a new line
* character (\n).
* </p>
* <p>
* This method assumes that every element in the specified array contains a
* valid reference to an <code>Employee</code> object.
* </p>
*
* @param array
* an array that contains objects of class {@link Employee}.
* @return the string representation of the specified array
*/
public static String displayAll(Employee[] array) {
if (array.length == 0)
return "";
String result = "";
for (int i = 0; i < array.length; i++) {
result += "Employee[" + array[i].getId() + "," + array[i].getName()
+ "," + array[i].getSalary() + "]";
if (i < array.length - 1)
result += "\n";
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -