From: Jeroen L. <lap...@gm...> - 2013-05-16 16:07:35
|
Hi there fellow developers, I'm giving you hereby my answers to the Java Test that I got from Till Schäfer. My answers to the SH test will follow since I'm typing this from my vacation adress in not-yet-so-sunny Barcelona on a Spanish keyboard. //Due to my vacation adress the following code is not compiled. It's unfortunate but it might even give you some more insight ;) With kind regards, Jeroen Lappenschaar applying for the Google Summer of Code ************ *Java Tasks* ************ *** Off Mean *** Complete the following method, which calculates the mean for all odd indices of the array. /** * Returns the mean for all odd indices of the given array. * Returns 0.0 if array size is 0. */ public float getOddMean(float[] numbers) { if(numbers.length == 0) return 0.0; float mean = 0.0; int j = 0; for(int i = 1; i < numbers.length; i+=2){ mean += numbers[i]; j++; } return mean/j; } *** End Contracts *** Complete the following method of the class 'Division'. It should remove all employees, which contract ends before the the given date and return them as list. /** * Removes all employees which contract ends before the given date and returns these employees */ public List<Employee> endContracts(Date date) { List<Employee> removed = new ArrayList<Employee>(); for(Employee employee : employees) if(employee.getEndOfContract().before(date)) { removed.add(employee); employees.remove(employee); //this uses the hash method described below } return removed; } *** Protect Employees *** The method 'getEmployees' allows to manipulate the content of the private attribute 'employees'. Modify the method 'getEmployees' in such a way that this is not possible anymore. /** * Returns an *unmoditiable* list of employees */ public List<Employee> getEmployees() { return Collections.unmodifiableList(employees); } *** Change monthlyWage *** Add a method to modify the monthly wage of an employee. Make sure that the class 'Division' is the only class with access to this method. Note that 'Division' is the only other class in the package of the class 'Employee'. //package private void setMonthlyWage(int newWage) { montlyWage = newWage; } *** Equal Employees *** Provide a standard way to check the equality of two employees. Two employees should be equal iff the personnelNumber is equal. public boolean equals(Object object) { if(! object instanceof Employee) return false; Employee employee = (Employee) object; return employee.getPersonnelNumber() == personnelNumber(); } //together with equals we also change the hashcode function public int hashCode() { return personnelNumber; } *** Generics *** Change the class node in such a way that it can store a content of arbitrary type. Make sure that the all nodes in a connected tree can only have the same content type. public class Node<E> { private Node<E> parent; private List<Node<E>> children; private E content; public Node(E content) { this.children = new ArrayList<Node<E>>(); this.content = content; } public void addChild(Node<E> newChild) { newChild.parent = this; children.add(newChild); } public E getContent() { return content; } } |