Help: Some Methods


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

Task 1

Create an Array variable in the Company class to hold a collection of Employee objects. Also create a variable to track the number of Employees. Both should have initializers.
Create the Array variable employees, initialize it to a new Array of three elements. Create the integer variable count and initialize it to zero.

Task 2

Write the addEmployee method of Company. It will add an Employee into the array. Be sure that it can handle cases where the array is full.
If the array becomes full (determined by comparing the array length with the number of employees), a new larger array is created and the contents of the old one copied into it. The employees variable is set to point to the new array, then the new Employee is added to it. The employee count variable is then incremented.

Task 3

Implement a toString method for Company. It should return a String containing the name of the company, then iterate through the array of employees, printing each.
Use a StringBuffer, as in the Magercise on strings. Append the name of the company to the buffer. Then loop through the employees array, calling toString on each and appending the result to the buffer. At the end, call toString on the buffer and return the result.

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