Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. * This class contains methods to process arrays of {@link Employee} objects.
  2. *
  3. * @author LiGuoLiang
  4. * @version 1.0.0
  5. * @see Employee
  6. */
  7. public class EmployeeArray {
  8.  
  9. /**
  10. * Creates an array with three objects of class {@link Employee}.
  11. * <p>
  12. * The first element of the array is the object
  13. * <code>first</code>, the second element of the array is
  14. * the object <code>second</code>, and the third element of the
  15. * array is the object <code>third</code>.
  16. * </p>
  17. *
  18. * @param first a {@link Employee} object.
  19. * @param second a {@link Employee} object.
  20. * @param third a {@link Employee} object.
  21. * @return an array with the objects <code>first</code>,
  22. * <code>second</code>, and <code>third</code>.
  23. */
  24. public static Employee[] makeArray(Employee first, Employee second,
  25. Employee third) {
  26.  
  27.  
  28. /* PLACE YOUR CODE HERE */
  29. Employee[] employee = new Employee[3];
  30. employee[0] = first;
  31. employee[1] = second;
  32. employee[2] = third;
  33.  
  34. return employee; // REMOVE; USED SO THIS FILE COMPILES
  35. }
  36.  
  37. /**
  38. * Creates a new array from the specified array of {@link Employee}
  39. * objects.
  40. * <p>
  41. * The elements in the new array have the same order as those in
  42. * the specified array.
  43. * </p>
  44. *
  45. * @param array an array that contains objects of class {@link Employee}.
  46. * @return a <i>new</i> array of the objects in the specified array.
  47. */
  48. public static Employee[] copyArray(Employee[] array) {
  49.  
  50.  
  51. /* PLACE YOUR CODE HERE */
  52. Employee[] employee = new Employee[array.length];
  53. for(int index=0;index<employee.length;index++) {
  54. employee[index] = array[index];
  55. }
  56.  
  57. return employee; // REMOVE; USED SO THIS FILE COMPILES
  58. }
  59.  
  60.  
  61. /**
  62. * Returns the {@link Employee} object with the specified ID.
  63. *
  64. * @param array an array that contains objects of class {@link Employee}.
  65. * @param id an employee ID.
  66. * @return The {@link Employee} object with the specifed
  67. * ID. Returns <code>null</code> if there are no employees
  68. * in the specified array with the specifed ID.
  69. */
  70. public static Employee getEmployee(Employee[] array, int id) {
  71.  
  72.  
  73. /* PLACE YOUR CODE HERE */
  74. for(int index=0;index<array.length;index++) {
  75. if(array[index].getId()==id){
  76. return array[index];
  77. }
  78. }
  79. return null; // REMOVE; USED SO THIS FILE COMPILES
  80. }
  81.  
  82. /**
  83. * Returns the number of employees whose salary is higher than the specified
  84. * dollar amount.
  85. *
  86. * @param array an array that contains objects of class {@link Employee}.
  87. * @param amount a dollar amount.
  88. * @return the number of employees whose salary is higher than the
  89. * specified dollar amount.
  90. */
  91. public static int countHigherSalaries(Employee[] array, double amount) {
  92.  
  93. /* PLACE YOUR CODE HERE */
  94. int number = 0;
  95. for(int index=0;index<array.length;index++) {
  96. if(array[index].getSalary()>amount) {
  97. number++;
  98. }
  99. }
  100. return number; // REMOVE; USED SO THIS FILE COMPILES
  101. }
  102.  
  103. /**
  104. * Returns the sum of the salaries of the employees in the specified
  105. * array.
  106. *
  107. * @param array an array that contains objects of class {@link Employee}.
  108. * @return the sum of the salaries of the employees in the specified
  109. * array.
  110. */
  111. public static double sumSalaries(Employee[] array) {
  112.  
  113.  
  114. /* PLACE YOUR CODE HERE */
  115. double sum = 0.0;
  116. for(int index=0;index<array.length;index++) {
  117. sum = sum + array[index].getSalary();
  118. }
  119. return sum; // REMOVE; USED SO THIS FILE COMPILES
  120. }
  121.  
  122. /**
  123. * Obtains the highest salary in the specified array.
  124. *
  125. * @param array an array that contains objects of class {@link Employee}.
  126. * @return the highest salary in the specified array.
  127. */
  128. public static double getHighestSalary(Employee[] array) {
  129.  
  130.  
  131. /* PLACE YOUR CODE HERE */
  132. double highestSalary = 0.0;
  133. for(int index=0;index<array.length;index++) {
  134. if(array[index].getSalary()>highestSalary) {
  135. highestSalary = array[index].getSalary();
  136. }
  137. }
  138.  
  139. return highestSalary; // REMOVE; USED SO THIS FILE COMPILES
  140. }
  141.  
  142. /**
  143. * Increases the salary of every employee in the specified array by the
  144. * specified amount.
  145. *
  146. * @param array an array that contains objects of class {@link Employee}.
  147. */
  148. public static void increaseAll(Employee[] array, double amount) {
  149.  
  150.  
  151. /* PLACE YOUR CODE HERE */
  152. for(int index=0;index<array.length;index++) {
  153. array[index].setSalary(array[index].getSalary()+amount);
  154. }
  155. }
  156.  
  157. /**
  158. * Returns a string representation of the specified
  159. * {@link Employee} array.
  160. * <p>
  161. * Uses the method <code>toString</code> in class <code>Employee</code>
  162. * to obtain the string representation of each object in the array.
  163. * </p>
  164. * A new line character ( \n ) separates the string representations
  165. * of each <code>Employee</code> object. For example:
  166. * </p>
  167. * <pre>
  168. * Employee[102,Mary Jones,68250.0]\n
  169. * Employee[101,Joe Smith,36000.0]\n
  170. * Employee[103,Richard Wong,92175.0]
  171. * </pre>
  172. * <p>
  173. * Note that the string returned does <i>not</i> end with a new line
  174. * character (\n).
  175. * </p>
  176. * <p>
  177. * This method assumes that every element in the specified array
  178. * contains a valid reference to an <code>Employee</code> object.
  179. * </p>
  180. *
  181. * @param array an array that contains objects of class {@link Employee}.
  182. * @return the string representation of the specified array
  183. */
  184. public static String displayAll(Employee[] array) {
  185.  
  186.  
  187. /* PLACE YOUR CODE HERE */
  188. String display = "";
  189.  
  190. if(array.length!=0) {
  191. for(int index=0;index<array.length-1;index++) {
  192. display += array[index].toString()+"\n";
  193. }
  194. display += array[array.length-1].toString();
  195. }
  196.  
  197. return display; // REMOVE; USED SO THIS FILE COMPILES
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement