Results 1 to 14 of 14

Thread: Aggregation !!

  1. #1
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Aggregation !!

    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 !!



    Java 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: -

    Java 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).
    Last edited by Sary; 04-14-2010 at 02:42 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sary View Post
    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 !!
    This means that you're not implementing the correct compareTo method. First of all, do you state that the class implements Comparable? If so, then compareTo must take an Object parameter to match the interface's signature, not an Employee parameter. If on the other hand you have the class implement the generic Comparable<Employee> interface, then compareTo should take an Employee parameter.
    Last edited by Fubarable; 04-14-2010 at 02:49 AM.

  3. #3
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Before you get too far ahead, you should complete the first part of the assignment.
    I know that, dude ..
    with respect to the first part u're talking about ..
    I posted my question about it ..
    it's when I implement the comparable interface in the following form :
    public class Employee implements Comparable{
    I get an error talking about something called abstract !

  4. #4
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    then compareTo must take an Object parameter to match the interface's signature, not an Employee parameter.
    but in the way i've done, "s" is an object of type Employee ! so it's an object parameter !

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sary View Post
    but in the way i've done, "s" is an object of type Employee ! so it's an object parameter !
    The method signature has to match exactly. So it can't be an Employee parameter (though inside of the method you would usually cast it to an Employee object). Please note the differences between using a generic vs a non-generic Comparable interface:

    The non-generic version
    Java Code:
    class ImplementsNonGenericComparable implements Comparable {
    
      // note the parameter type
      public int compareTo(Object other) {
        ImplementsNonGenericComparable fu = 
           (ImplementsNonGenericComparable) other;
        // ... more code goes here
        return //... -1, 0, or 1
      }
      
    }


    Now the generic version:
    Java Code:
    class ImplementsGenericComparable implements Comparable<ImplementsGenericComparable> {
    
      public int compareTo(ImplementsGenericComparable other) {
        // some code goes here, no need to cast 
        return //... -1, 0, or 1
      }
      
    }

  6. #6
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    dude, u may be talking about things I didn't study about !
    I know the interface in the way I've done it in the code above, but the problem is that as I said, I am having an error talking about abstract !
    I have no idea about generic and non-generic ..
    so, simply .. how can I correct my error in the first part of the assignment ?!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sary View Post
    dude, u may be talking about things I didn't study about !
    I know the interface in the way I've done it in the code above, but the problem is that as I said, I am having an error talking about abstract !
    I have no idea about generic and non-generic ..
    so, simply .. how can I correct my error in the first part of the assignment ?!
    "Dude", it has nothing to do with "abstract" and everything to do with the compareTo method not being correct. If you can't use a generic Comparable, then just ignore that example, but don't ignore the other example. Put as simply as possible, in your case the compareTo method MUST take an Object parameter, nothing more, nothing less, not an Employee parameter, not a Fubar parameter, but an Object parameter pure and simple. If that doesn't get rid of your error message, then post your best attempt at coding the class with it implementing Comparable, and we'll see if we can help you out based on this code.

    Best of luck.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    As Fubarable told you above, when you implement the Comparable interface, you must implement this method:
    Java Code:
            public int compareTo(Object other) {
                    int result = 0;
                    // TODO: change result to a negative integer if this is less than other
                    //     change result to positive integer if this is greater than other
                    //     leave result at zero if this is equal to other
                    ...
                    return result;
            }
    Note that the parameter must be of type Object, not of type Employee. This is what Fubarable has been trying to explain to you. You may throw a CastClassException if the object passed to your method is not an Employee (or you can go ahead and decide somehow if it's greater or less or equal, if it makes sense to do so).

    -Gary-

  9. #9
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    ok guys .. thanks a lot .. got the idea ..
    but what about the 2nd part of the question .. concerning the aggregation !

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sary View Post
    ok guys .. thanks a lot .. got the idea ..
    but what about the 2nd part of the question .. concerning the aggregation !
    If I'm understanding the assignment correctly, all aggregation means is that the second class doesn't extend Employee but rather contains one or more Employee objects (or collections). You may be trying to make it out to be more complex than it is. Your best bet is to try to take a stab at implementing that second class and see what comes up.

  11. #11
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    I started with the 2nd class, bro ..
    and i created 3 employee objects .. which refer to those objects in the Employee class .. that's what is called Aggregation .. but my main problem is the add method .. how to write it !!

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sary View Post
    I started with the 2nd class, bro ..
    and i created 3 employee objects .. which refer to those objects in the Employee class .. that's what is called Aggregation .. but my main problem is the add method .. how to write it !!
    I don't see code with a class called Company anywhere posted anywhere in this thread. If I missed it, please point it out to me. You do understand that the Company as described does not have a main method.

  13. #13
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    Java Code:
    package employee;
    
    
    public class Company {
        
        private String name;
        
        Employee e1;
        Employee e2;
        Employee e3;
        
        public Company (String n)
        {
            name = n;
        }
        
        public int Add (Employee e)
        {
            
        }

    I reached this ..
    I have a problem with the Add method .. how to do it !

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Your instructor should be horsewhipped for making you do such a goofy assignment. But since you are required to use three separate variables to hold your Employees, you'll need to use a switch or an if/else block in your addEmployee method (don't call it Add() -- you can call it add() if you don't like addEmployee()).

    I would use a numEmployees instance variable, initialize it to zero and increment it each time you add an Employee. You'll need that variable for your toString() method anyway. Use that variable to decide which e variable to assign your Employee to, and maybe return a -1 if you are already at 3 employees.

    -Gary-

Similar Threads

  1. UML association,aggregation etc generator
    By alexander.s in forum New To Java
    Replies: 1
    Last Post: 09-18-2008, 07:27 PM
  2. inheritance and aggregation
    By java_fun2007 in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 02:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •