• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism, overriding, homework help  RSS feed

 
Michael Kim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help with an assignment for my java course. I'm struggling with the idea of polymorphism and overriding methods and abstract methods.

For this assignment I'm told to make changes to code that is in the textbook with the following:

Make the following updates to Firm.java
You MAY NOT use any of the collection classes (such ArrayList) for this assignment.  You must 'roll your own' arrays.

1) Modify the Firm program such that it expands its use of polymorphism by using an interface called Payable.  You will move pay method from StaffMember into Payable.

2) Modify Firm such that all employees can be given different vacation options depending on their classification. Provide an abstract method in StaffMember called vacation that returns the number of vacation days a person has.  Give all Employees a standard number of vacation days (14) using a constant STANDARD_VACATION.

a) Then override the vacation method in the various Employee classes as follows:
- Employee: STANDARD_VACATION
- Hourly: STANDARD_VACATION - 7
- Executive: STANDARD_VACATION + extraVacation
    **NOTE**: You will need to add extraVacation as instance variable to Executive and pass
    it in as an argument to the constructor.
- Volunteer** (who are not Employees): 0
b)  Call vacation method inside the loop already used by payday method so we see each StaffMembers vacation days.

3) Rewrite the Sorting class so that both sorting algorithms put the values in descending order. 

4)  Sort the array of StaffMembers based on name using the updated selection OR insertion sort from Sorting.java. Put the sort method in the class that has direct access to the array. Then call the sort in main before calling payday        

**Hints on Implementation**

Constructor for Executive needs to be passed the number of vacation days.  No other StaffMember needs to have its constructor updated.

All StaffMembers need to override the vacation method

Use a constant for VACATION_DAYS. This way if the Firm becomes more generous in future, we only need to update one line of code.

StaffMember needs to OVERRIDE compareTo method (we will use names for comparint).  This means you need to implement the Comparable interface so the Sorting.java methods work.

Below is the code from the textbook that I am to implement the changes to (this is the original code, not what I've done so far.) I'll post what I've done so far, that I understand, in a reply below so as to not mix up all the code.

Firm.java


StaffMember.java


Employee.java


Hourly.java


Executive.java


Volunteer.java


Sorting.java


Sample Output

 
Michael Kim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what I've done so far is creating the payable interface, and moving the pay method into the Payable interface. I've also extended the StaffMember class for Employee, Hourly, Executive, and Volunteer, while also implementing Payable.
I created a vacation method inside StaffMember and tried to override it in each of the extended classes.

What I need checking on is if I'm doing the vacation method correctly, and overriding them in the corresponding classes correctly.  Also, could you check if I've correctly passed the extraVacation instance variable to the Executive constructor.  And lastly if I correctly called the vacation method in the payday loop inside the Staff class.

Payable Interface


StaffMember.java


Employee.java


Hourly.java


Executive.java


Volunteer.java


Staff.java

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed that you haven't gotten any responses yet and and I'd first like to tell you why I think that is.

1) Your questions vague and unfocused.  "Check that I've done this right" isn't the best question.  Better questions are specific. 

2) Don't post code with compile errors unless your question is about compile errors.  Fix those and repost.

3) Write some tests and see if the code works as you expect.  If it doesn't, post the test code and what exactly happened.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

One of the things I suggest you test first is what happens when you create an executive instance and give it a bonus of zero.
Nicely‑formatted code, but, don't double‑space code. That doesn't make it easier to read, but more difficult.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!