Problem Statement
You are given a collection containing Employee objects. You will need to compare these objects to an object from an input stream as described below.
Given class: EmployeePayroll
Contains a collection of employees and a stream that contains a test Employee. A description of your task follows the class definition.
class EmployeePayroll {
/**
* Instantiates the payroll by using the appropriate ID given to you.
*
* @param employeeCollectionID The ID of the Payroll to load.
*/
public EmployeePayroll(int employeeCollectionID);
/**
* Returns the Employee objects listed in this payroll.
* See the Employee class.
*
* @return A collection of 5 to 125 Employee objects, inclusive.
*/
public Collection<Employee> getCollection();
/**
* Gets a Stream containing the Employee object compare against the collection.
*
* @return An ObjectInputStream with the first object guaranteed to be an Employee.
* @throws IllegalStateException if the stream is currently unavailable, call again
* until no exception is thrown
*/
public ObjectInputStream getStream();
}
Given class: Employee
Represents Employee. The Employee class has properties and methods that are not shown here, including appropriate Object overrides. Note that employees with identical names are not necessarily equal.
class Employee implements Serializable {
/**
* Gets the name of the employee. Note: do not rely on the name
* to determine whether two Employee objects are equal.
*
* @return a name consisting of eight characters in the range 'a' to 'z' (lowercase letters)
*/
public String getName();
}
DEFINITION
You must define a public class EmployeeProcessor with public method String collisionNames(int employeeCollectionID, int hashSize). employeeCollectionID is between 0 and 65535, inclusive. hashSize is between 10 and 50, inclusive.
Implement collisionNames as follows:
Instantiate the provided EmployeePayroll class by passing employeeCollectionID to EmployeePayroll(int). Call getCollection() on the resulting object to obtain a collection of Employee objects.
Use getStream() to obtain an ObjectInputStream. The first object in the stream will be an instance of the Employee class. Read this object and compare it to the objects in the collection.
Call getName() on each Employee from the collection that is not equal to the Employee from the stream, but that has the same hash code modulo hashSize.
Return a String containing all the unique names. This string must be sorted alphabetically and separated by commas.
Definition
- Class:
- EmployeeProcessor
- Method:
- collisionNames
- Parameters:
- int, int
- Returns:
- String
- Method signature:
- String collisionNames(int employeeCollectionID, int hashSize)
- (be sure your method is public)
Notes
- Employees with identical names are not necessarily equal.
- You can calculate X modulo Y with the following Java expression: X % Y
Constraints
- employeeCollectionID will be between 0 and 65535, inclusive.
- hashSize will be between 10 and 50, inclusive.
Examples
400
10
Returns: "eugfxmei"
After we instantiate EmployeePayroll with the specified ID, getCollection() returns a collection of five Employee objects. Their names as returned by getName() are the following, in order of iteration: "mjlfiuhd", "eugfxmei", "kpgjvbxb", "iopauupl", "xkeqskqn". The first object we read from the ObjectInputStream returned by getStream() has the name "kpgjvbxb", and its hash code modulo 10 is 9. There is just one object in the collection that is not equal to the object from the stream but that has the same hash code modulo 10. Its name is "eugfxmei", which we put into the return string.53311
39
Returns: "ffoitymf,mqyykpwu"
Here we have seven employees in the collection, with names as follows: "ffoitymf", "snxszkxp", "snxszkxp", "mqyykpwu", "ohitgimk", "gwivlhvu", "ffoitymf". The name of the object from the stream is "ffoitymf" and its hash code modulo 39 is 33. The first object in the collection is equal to the object from the stream, so we don't consider it any further. There are two objects in the collection that are not equal to the object from the stream but have the same hash code modulo 39: the fourth one in the list above, with the name "mqyykpwu", and the last one, with the name "ffoitymf". We sort these names alphabetically and join them with commas.14894
31
Returns: ""
Don't forget that getStream() may throw an IllegalStateException. There are several employees in the collection with duplicate names, but none that satisfy our requirements.17682
12
Returns: "kscjmjax,rntovjiw"
There are three objects with the name "rntovjiw" that meet our criteria, but we must include the name only once in the return string.3099
12
Returns: "hgbudbjy,iwfjeghw,lcbwkcmh,ofpmescb,trsnlebu,ycxaqioi,zbvzemrs,zhyqomtk"
Here we have a collection of 109 Employee objects.