Help: Equality in Java


Help is available for each task, or you can go straight to the solution source code.

Task 1

As you did in the previous Magercise, add a method findEmployee to the Company class, in the Magelang/JavaBasics package. It takes as its argument an Employee and returns the same object if it is found to be an employee of the Company. Otherwise it returns null.
Use a for loop to loop through the employee array. Compare each Employee to the given one using ==. Return the given Employee if the test is true. Return false after the loop is done, indicating that no match was found.

Task 2

Add a similar method called findEmployeeNamed. This method takes as its argument a String name and tries to find an Employee of that name. It returns the Employee object if successful.
Use a loop similar to the one above, but use the equals method to compare the names of the Employees to the given name.

Task 3

Add code in the main method of the EqualityTest class to use these two new methods to search for some employees.
Add a call to findEmployee using a known Employee of the Company. Also add a call to findEmployeeNamed with the name of a known Employee. You can also add seaches that will fail.

Task 4

Run your solution via the EqualityTest class.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.