Create a class called Employee that implements the Comparable interface with four data members: int id, String name, double salary, and int counter used to count how many employees are in a company. The methods are: copy constructor, getter and setter methods, a print method (that outputs the id, name, and salary), and the method compareTo that is used to check if two employees have the same salary or not (This method returns 0 if the salaries are equal, 1 if the first salary is greater than the other, and -1 if smaller . Write a test drive (class containing a main method) in which you create 3 Employee objects and use the different methods with them.
I solved this part in this way: -
I have a question here ..
the question is asking to implement the comparable interface, but I directly used the compareTo method without implementing the comparable interface, because when I implement it next to the statement: public class Employee, I get an error talking about abstract stuff !!
Code:
package employee;
public class Employee {
private int ID;
private String name;
private double salary;
private static int counter=0;
public Employee (int i, String n, double s)
{
ID = i;
name = n;
salary = s;
counter++;
}
public int getID()
{
return ID;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public static int getCounter()
{
return counter;
}
public void setID(int i)
{
ID = i;
}
public void setName(String n)
{
name = n;
}
public void setSalary(double s)
{
salary = s;
}
public void print()
{
System.out.println("The employee's ID is: " + ID + ", his name is: "
+ name + ", and his salary is: " + salary);
}
public int compareTo (Employee s)
{
if (salary==s.salary)
return 0;
else if (salary > s.salary)
return 1;
else
return -1;
}
}
the main: -
Code:
package employee;
public class Main {
public static void main(String[] args) {
Employee e1 = new Employee (1120, "Jack", 2000.00);
Employee e2 = new Employee (1121, "Dan", 3000.00);
Employee e3 = new Employee (1122, "Elie", 2000.00);
System.out.println("The information of Employee e1 is: ");
e1.print();
System.out.println("The ID of Employee e2 is: " + e2.getID());
e3.setID(1123);
e3.setName("Bob");
System.out.print("The ID of Employee e3 is: " + e3.getID());
System.out.println(", and his Name is: " + e3.getName());
System.out.println("The number of employees in the company are: "
+ Employee.getCounter());
System.out.println(e1.compareTo(e2));
System.out.println(e2.compareTo(e3));
System.out.println(e1.compareTo(e3));
}
}
this is the 2nd part of the question .. It includes the concept of aggregation .. but I didn't get the idea well .. can I get some help ?
Create a class called Company with the following four data members: String name (company name), and three employee objects e1, e2, and e3 (which are objects references from the class Employee in problem 1). The company might have 1, 2 or 3 employees. The methods of the class are: a copy constructor that initializes the name only, a method called add that adds one employee to the company each time it is called, and a print method that outputs the company name, the information of all employees, and the number of employees in the company. Write a test drive (class containing a main method) in which you create a company and add to it two employees and use the different methods of the class.
Hint: You might need to use the null constant (but the problem can be solved without it). If an object reference does not refer to an object then it is equal to null. We can test if an object reference is null by writing for example: if (e1= =null) or if (e1!=null).

