Help on an Employee clas with inheritance

I need help with some parts of this class, and see if my code is good

Page 1 of 1

10 Replies - 3071 Views - Last Post: 17 October 2010 - 07:07 PM Rate Topic: -----

#1 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Help on an Employee clas with inheritance

Posted 16 October 2010 - 09:26 PM

Hello everyone,

I have an assignment which is to make an abstract employee class with some subclasses, which I've finished now but to finish, I need some more help. First, in the employee client, the user is supposed to input the category of the employee, which means that that value is what chooses which subclass that employee will be a part of, but I haven't an idea of how to do so. Second, I have to implement a compareTo method from the Comparable interface, and I have to make it so that it sorts primarily by category, then by the employee's pay rate. I may need some help later but so far, these are the things I need the answers for most.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Help on an Employee clas with inheritance

#2 cybiko123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 10

Re: Help on an Employee clas with inheritance

Posted 16 October 2010 - 09:31 PM

Why not assign each category a numerical value based on rank (e.g. CEO/President is 1, Vice President is 2, etc.)? You then would be able to compare the categories, and fall back to pay rate when necessary.
Was This Post Helpful? 0
  • +
  • -

#3 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Re: Help on an Employee clas with inheritance

Posted 16 October 2010 - 09:34 PM

actually, that is what I had in mind about assigning each category a number but I don't know how to implement that. the client supplies the category number to the constructor in the Employee class, but that's as far I know. How would you code the constructor?
Was This Post Helpful? 0
  • +
  • -

#4 cybiko123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 10

Re: Help on an Employee clas with inheritance

Posted 16 October 2010 - 09:41 PM

The category would just be another field. So in your constructor, you assign it like you would any other field. Like so:

public class Employee {

...other code...

int category;

...other code...

public Employee(String name, int category, double payRate)
{
this.category = category;
...other code...
}

}



Was This Post Helpful? 0
  • +
  • -

#5 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Re: Help on an Employee clas with inheritance

Posted 17 October 2010 - 08:36 AM

Alright. Now what I need help with is how to implement the CompareTo method here. What do I do for that?
Was This Post Helpful? 0
  • +
  • -

#6 cybiko123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 10

Re: Help on an Employee clas with inheritance

Posted 17 October 2010 - 09:04 AM

View PostSilverMage, on 17 October 2010 - 09:36 AM, said:

Alright. Now what I need help with is how to implement the CompareTo method here. What do I do for that?


compareTo is an int function that takes an Object as a parameter and returns one of three types of values:

A negative number, if the object you have is smaller than the one passed as a parameter.
Zero, if the object you have is equal to the parameter.
A positive number, if the object you have is greater than the parameter.

Keep in mind that the parameter must be an Object; it cannot be, for example, an Employee.
Was This Post Helpful? 0
  • +
  • -

#7 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Re: Help on an Employee clas with inheritance

Posted 17 October 2010 - 11:59 AM

All right, thanks cy. Now, I got what I believe is my last problem to solve. In the client, I want to use a method from a subclass but can't. Here is my code

                        Employee worker=null;
                        
                        switch(category)
                        {
                            case 0: worker=new SalariedEmployee(idNbr,rate); break;
                            case 1: worker=new WeeklyEmployee(idNbr,rate); break;
                                           //Here is where I am aiming to use a method from the WeeklyEmployee subclass.
                            case 2: worker=new DailyEmployee(idNbr,rate); break;
                            default:
                        }


Actually, the method is from abstract subclass ClockedEmployee, which has the concrete classes WeeklyEmployee and DailyEmployee, but when I take a look at the available methods to use in the client with worker, I don't see the method I need from ClockedEmployee.

This post has been edited by SilverMage: 17 October 2010 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 cybiko123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 10

Re: Help on an Employee clas with inheritance

Posted 17 October 2010 - 01:37 PM

View PostSilverMage, on 17 October 2010 - 12:59 PM, said:

All right, thanks cy. Now, I got what I believe is my last problem to solve. In the client, I want to use a method from a subclass but can't.


What happens if you typecast the object as a WeeklyEmployee when calling the method?
Was This Post Helpful? 0
  • +
  • -

#9 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Re: Help on an Employee clas with inheritance

Posted 17 October 2010 - 02:37 PM

Yes! casting works! I tried it out earlier but I had to cast it using double parentheses and it works now! thanks man
Was This Post Helpful? 0
  • +
  • -

#10 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Re: Help on an Employee clas with inheritance

Posted 17 October 2010 - 03:07 PM

All I need help with now is with creating a double level sort with the compareTo method and how to work the FileReader. For the FileReader, I just need to know where I would save a file because our professor told us to download a text file which is to be read by the program, but told us nothing about where to save it to make it accessible to the program. Any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 cybiko123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-October 10

Re: Help on an Employee clas with inheritance

Posted 17 October 2010 - 07:07 PM

View PostSilverMage, on 17 October 2010 - 04:07 PM, said:

All I need help with now is with creating a double level sort with the compareTo method and how to work the FileReader. For the FileReader, I just need to know where I would save a file because our professor told us to download a text file which is to be read by the program, but told us nothing about where to save it to make it accessible to the program. Any ideas?


By double sort, do you mean compare the categories, and those being equal compare the pay rate? If so, just use two nested if/else statements and some return statements.

For file I/O, I always use absolute paths. Try that and let me know how it goes.

This post has been edited by cybiko123: 17 October 2010 - 07:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1