Create a Faculty subclass of Employee which has the additional data of rank. Rank could be one of Assistant Professor, Associate Professor, or Professor. Create a Staff subclass of Employee which has the additional data of Category (Full or Part time). Provide suitable constructors. Each of these sub classes also have a change_status() method that changes (for example by promoting) the rank or category respectively. Add a change_status() method to the Employee base class that simply outputs a message “Cannot change this Employee’s status”.

Demonstrate dynamic binding in your main program. Declare an array of Employees (compile time type) but assign different subclasses to each array element (run time type). Show that get_info() and change_status() uses the methods determined by their run time types and displays all the relevant data.

The first part of the homework assignment was to create an object employee that has a name and salary and change in salary. I did that now the assignment above is the second part.

Im not sure what im doing wrong i've read through the book and everything. Im also confused on what dynamic binding is and how it differs from polymorphism. Does my code demonstrate dynamic binding or am I getting it confused with polymorphism. Any help would be appriciated thanks.

03-24-2011, 07:51 AM

DarrylBurke

The code you posted doesn't correspond to the errors. When seeking help, make sure that you post both the latest iteration of your code and the errors you see from the very same code. Also, remove any excess vertical white space -- it's annoying to have to scroll through and makes it more difficult to read the code.

When writing a class, if you don't provide a constructor the compiler creates a default, no-argument constructor. Note that if you do provide a constructor this no-argument constructor is not created.

In every constructor, the first line may be a explicit call to another constructor of the same class (using the this keyword) or of the super class (using the super keyword). If the first line is not an explicit call to another constructor, the compiler inserts a call to the super class's default (no-argument) constructor.

You have an Employee class that doesn't have a default constructor, since you defined a constructor that takes two parameters. In the two classes that extend Employee, you have not however provided any constructor so the compiler inserts a default constructor that attempts to call the default constructor of the super class -- Employee -- but can't find one.

db

03-30-2011, 09:54 PM

mackavelirip

Thanks for your reply if I understood what you said i made the changes but now get new errors. This is what I have so far.

when you get an error like that, please follow the line number to spot the error and if then you don't see what it is and can't correct it, all we really need is that line & probably the method that line falls in to spot the error - not your whole code as most people have lives and wont read it anyway.

furthermore, you say that you understand what darryl told you but clearly you havent.
Note: that the compiler error says cannot find symbol Employee()
NOT cannot find symbol Employee(java.lang.String, double)

Quote:

In every constructor, the first line may be a explicit call to another constructor of the same class (using the this keyword) or of the super class (using the super keyword). If the first line is not an explicit call to another constructor, the compiler inserts a call to the super class's default (no-argument) constructor.

You have an Employee class that doesn't have a default constructor, since you defined a constructor that takes two parameters. In the two classes that extend Employee, you have not however provided any constructor so the compiler inserts a default constructor that attempts to call the default constructor of the super class -- Employee -- but can't find one.

Alright I looked up how to use the super method and got it to run thank you very much. I have one more question. When I run the program, for the first employee in the array everything works fine. I ask for the name, salary and change in salary and it works. Then when it goes to the next person in the array, it says whats the name of your employee but instead of letting you type something it skips it and goes straight to the salary. I was wondering why that is.

Heres my updated code.

Heres what the output looks like. I bolded the part where it skips the name.

Quote:

What is the name of your employee?
Fred
What is their salary?
15000
How much is the change in salary?
5000
Is your employee getting a raise[Y/N]?
y
Name: Fred
There new salary is 20000.0
Change in salary: 5000.0
Cannot change this employee's status.What is the name of your employee? //this is where it skips
What is their salary?
15000
How much is the change in salary?
1000
Is your employee getting a raise[Y/N]?
n
Name:
There new salary is 14000.0
Change in salary: 1000.0
1. Professor
2. Assistant Professor
3. Associate Professor
Enter the number that corresponds with your employees new job title.
1
Your employee is now a ProfessorWhat is the name of your employee? //this is where it skips
What is their salary?
20000
How much is the change in salary?
2000
Is your employee getting a raise[Y/N]?
y
Name:
There new salary is 22000.0
Change in salary: 2000.0
1. Full-time
2. Part-time
Enter your employees category with the corresponding number.
2
Your employee is part-time

And I would like to say thanks for your guys help this is my first programming class, although its fun its very challenging to me and I appreciate you guys taking your time to help me, sorry if I am a nuisance.

03-31-2011, 03:34 AM

Fubarable

I think your problem is that your Scanner object, console doesn't always handle the end of line token. I suggest that wherever you have console.next(), if possible change it to console.nextLine(), and wherever you have console.nextInt() or console.nextDouble(), follow this with a call to console.nextLine() just to handle and swallow the end of line token.