Copy the lab materials to your account by entering
cp -r /home/Classes/Cs1/Labs/Lab14 .Everything you need for the lab exercises today is contained in this new directory.
First examine the Employee class. Look in particular at the parameters required for the constructor.
Next examine the code for the EmployeeAverage class. This program reads a list of employees from a text file named employees and creates a corresponding ArrayList of Employees. Compile this program, and note that there are compiler errors. Find the locations of these errors, determine what the problems are, and correct them.
1
Explain the compile errors and what you did to correct them.
Run your corrected program. You will see that it displays the employees in a JScrollPane. When a collection of one or more employees is selected, the text pane at the bottom of the window displays the average of their ages. Compile and run the program and test it by selecting various combinations of employees and determining their average ages. Be sure you can select both all the employees and none of them. Observe that when you select no employees, the text pane is erased.
Modify your code so that the bottom pane displays the average of the salaries of the selected employees in the form "Average salary ...", where the average is displayed in $#,##0.00 format,
2
Show us your modification and your running program.
Button averageBy[0] should be labeled "Age", and averageBy[1] should be labeled "Salary", with the "Age" button as as the default selection. Set the variable averageSelection (also a private data member) to averageBy[0] as well. Be sure to create a ButtonGroup object and add the two buttons to this object. (Why?)
You will need to add an itemStateChanged() method to your EmployeeAverage class, with an ItemEvent parameter. For now, leave the body of this listener empty. In addition, you will need to add the ItemListener interface name to your list of implemented interfaces in your EmployeeAverage class definition. Finally, you will need to add this as an ItemListener to each of the two buttons.
Compile and run your program. You should be able to see your buttons and select either one of them, although the selection will not have any effect.
3
Show us your running program
and explain the purpose of the ButtonGroup class.
Now implement the code for the itemStateChanged event handler. This code is called whenever you click on one of the radio buttons to change the selected average "Average by ..." choice. Remember that two events occur when you do so: one for the item that is deselected, and another for the new item that has been selected. If it is not a SELECTED event, ignore it.
When the event indicates that a button has been selected, get the event source from the ItemEvent parameter and set the private averageSelection variable to this button.
Modify your displayAverage() method so that if averageSelection is button zero, the average age is displayed in the text window in #0.0 format, otherwise the average salary is displayed in $#,##0.00 format. Do not put any other text in the text window.
Compile and test your program
4
Show us the changes you made and demonstrate your program.
Instead of having your average displayed at the bottom (SOUTH) part of your main window, move it into the same pane as your radio buttons. Your two radio buttons should appear first, then the text box.
Compile and run your program. When displaying average salaries, you may need to resize your main window so that the text box will fit on the same line as the radio buttons. (Or you can create your main window with a larger initial width.)
Finally, add a JButton labeled with "EXIT" and put it at the bottom (SOUTH) of your main window. Associate an ActionEvent handler with this button that will exit the program with a call to System.exit(0).
Compile and run your program
5
Show us your running program.
Modify your program so that
6
Show us the change you have made and your running program.
7
Show us that you have logged out, cleaned up, and pushed in your chairs
for this last checkpoint.