⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssd3exercise6.txt

📁 SSD3的练习6的答案.快期末考试了
💻 TXT
字号:
EXE 6

001 /**
002 * This class contains methods to process arrays of {@link Employee} objects.
003 *
004 * @author author name
005 * @version 1.0.0
006 * @see Employee
007 */
008 public class EmployeeArray {
009 
010 /**
011 * Creates an array with three objects of class {@link Employee}.
012 * <p>
013 * The first element of the array is the object
014 * <code>first</code>, the second element of the array is
015 * the object <code>second</code>, and the third element of the
016 * array is the object <code>third</code>.
017 * </p>
018 *
019 * @param first a {@link Employee} object.
020 * @param second a {@link Employee} object.
021 * @param third a {@link Employee} object.
022 * @return an array with the objects <code>first</code>,
023 *           <code>second</code>, and <code>third</code>.
024 */
025 public static Employee[] makeArray(Employee first, Employee second,
026                                     Employee third) {
027 
028    Employee[] employees = {first,second,third};
029    if(employees!=null){
030     return employees;
031    }
032    else return null;
033   
034 
035    // REMOVE; USED SO THIS FILE COMPILES
036 }
037 
038 /**
039 * Creates a new array from the specified array of {@link Employee}
040 * objects.
041 * <p>
042 * The elements in the new array have the same order as those in
043 * the specified array.
044 * </p>
045 *
046 * @param array an array that contains objects of class {@link Employee}.
047 * @return a <i>new</i> array of the objects in the specified array.
048 */
049 public static Employee[] copyArray(Employee[] array) {
050 
051    Employee[] employees = array;
052    if(employees!=null){
053     return employees;
054    }
055    else
056         return null; // REMOVE; USED SO THIS FILE COMPILES
057 }
058 
059 
060 /**
061 * Returns the {@link Employee} object with the specified ID.
062 *
063 * @param array an array that contains objects of class {@link Employee}.
064 * @param id an employee ID.
065 * @return The {@link Employee} object with the specifed
066 *           ID. Returns <code>null</code> if there are no employees
067 *           in the specified array with the specifed ID.
068 */
069 public static Employee getEmployee(Employee[] array, int id) {
070   
071 int i = 0;
072 int j = 0;
073 while(i<array.length&&id!=j){
074    j = array[i].getId();
075    i++;
076    }
077 if(id==j){
078    return array[--i];
079 }
080 else
081 {
082    return null;
083 }
084 
085      // REMOVE; USED SO THIS FILE COMPILES
086 
087 }
088 
089 /**
090 * Returns the number of employees whose salary is higher than the specified
091 * dollar amount.
092 *
093 * @param array an array that contains objects of class {@link Employee}.
094 * @param amount a dollar amount.
095 * @return the number of employees whose salary is higher than the
096 *           specified dollar amount.
097 */
098 public static int countHigherSalaries(Employee[] array, double amount) {
099         int i = 0;
100         int j = 0;
101    while(i<array.length){
102     if(array[i].getSalary()>amount){
103       j++;
104     }
105     i++;
106    }
107 
108    return j; // REMOVE; USED SO THIS FILE COMPILES
109 }
110 
111 /**
112 * Returns the sum of the salaries of the employees in the specified
113 * array.
114 *
115 * @param array an array that contains objects of class {@link Employee}.
116 * @return the sum of the salaries of the employees in the specified
117 *           array.
118 */
119 public static double sumSalaries(Employee[] array) {
120 
121    int i = 0;
122    double j = 0.0;
123 
124       while(i<array.length){
125     j += array[i].getSalary();
126     i++;
127       }
128    return j;
129    // REMOVE; USED SO THIS FILE COMPILES
130 }
131 
132 /**
133 * Obtains the highest salary in the specified array.
134 *
135 * @param array an array that contains objects of class {@link Employee}.
136 * @return the highest salary in the specified array.
137 */
138 public static double getHighestSalary(Employee[] array) {
139 
140     int j = array.length;
141     double[] s = new double[j];
142     int i = 0;
143     while(i<j){
144        s[i] = array[i].getSalary();
145        i++;
146     }
147     java.util.Arrays.sort(s);
148     return s[j-1];
149 
150 }
151 
152 /**
153 * Increases the salary of every employee in the specified array by the
154 * specified amount.
155 *
156 * @param array an array that contains objects of class {@link Employee}.
157 */
158 public static void increaseAll(Employee[] array, double amount) {
159 
160    int i = 0;
161    while(i<array.length){
162     double j = array[i].getSalary();
163     array[i].setSalary(j += amount); 
164     i++; 
165    }
166   
167 
168 }
169 
170 /**
171 * Returns a string representation of the specified
172 * {@link Employee} array.
173 * <p>
174 * Uses the method <code>toString</code> in class <code>Employee</code>
175 * to obtain the string representation of each object in the array.
176 * </p>
177 * A new line character ( \n ) separates the string representations
178 * of each <code>Employee</code> object. For example:
179 * </p>
180 * <pre>
181 * Employee[102,Mary Jones,68250.0]\n
182 * Employee[101,Joe Smith,36000.0]\n
183 * Employee[103,Richard Wong,92175.0]
184 * </pre>
185 * <p>
186 * Note that the string returned does <i>not</i> end with a new line
187 * character (\n).
188 * </p>
189 * <p>
190 * This method assumes that every element in the specified array
191 * contains a valid reference to an <code>Employee</code> object.
192 * </p>
193 *
194 * @param array an array that contains objects of class {@link Employee}.
195 * @return the string representation of the specified array
196 */
197 public static String displayAll(Employee[] array) {
198   
199    int i = 0;
200    String display = "";
201    if(array == null){
202     return null;
203    }
204    else {while(i<array.length){
205        display = array[0].toString() + "\n" + array[1].toString() + "\n" +
206      array[2].toString() + "\n" + array[3].toString();
207      i++;
208        }
209    }
210 
211    return display; // REMOVE; USED SO THIS FILE COMPILES
212 }
213 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -