Wednesday, February 29, 2012

Lab 15: Comparable

For this lab you will implement two classes.

An Employee class with a int ssn property and a String name property. The class implements the Comparable interface by comparing the ssn of the two employees. You can learn more about Comparable by reading the case study in page 632 of your textbook.

You will also implement a Department class which maintains an array of Employees. It implements the methods
  • add(String name, int ssn) which adds a new employee to the department. You can assume that at most 10 employees will be in a department
  • toString() returns a string of all the people in the department but sorted by ssn.

For example, the sample code below generates some random employees and them prints them out:
public static void main(String[] args) {
  Department d = new Department();
  for (int i=0; i < Math.random() * 10;i++){
   int n = (int)(Math.random() * 100);
   String name = "John" + n;
   d.add(name, n);
Here is the output of one run. Notice how they are all sorted. Every time you run it the numbers will be random but will (should) appear sorted.
John3 3
John16 16
John49 49
John61 61
John92 92
Remember to turn in both files to the

1 comment:

Jose Vidal said...

Yes, you can add more methods if you need them.