calculate salary problem  | |
April 6th, 2007, 01:27 PM
|
#1 (permalink)
| | Junior Member
Join Date: Mar 2007
Posts: 2
| (File handling, Abstract class, class, inheritance, and polymorphism) Among the people that a company employs are full-time and part-time workers. Consider a situation where we have a super class worker and the 2 subclasses part-time worker and full-time worker. Every worker has a name and a salary rate. Weekday working hours · For normal week days, a part-time worker gets paid the hourly wage for the actual number of hours worked, if the hours are at most 40. If the part time worker worked more than 40hrs, the excess is paid at time and a half. · The full time worker gets paid the hourly wage for 40 hours, no matter what the actual number of hours worked. Weekend working hours · A part time worker is paid at time and a half for every hour worked. · A full time worker is paid twice the regular rate every hour worked. Develop a java program to calculate the total weekly wage for all the full time and part time workers employed by the company. __________________________________________________ ___________________ I’ve already developed the Worker class: //Worker.java public abstract class Worker { protected String name; protected float salaryrate; //get first name public String getname() { return name; } public void setname(String n) { name = n; } //get salaryrate public double getsalaryrate() { return salaryrate; } public void setsalaryrate(float s) { salaryrate = s; } public abstract float calculate(int x); public abstract float calculate(int y, int z); } //end class Worker Full time worker subclass // Fulltime.java // Fulltime derived from Worker. public final class Fulltime extends Worker { //default constructor public Fulltime() { name = " "; salaryrate = 0.0f; } // parameterised constructors for class Fulltime public Fulltime (String n1, float salary) { name = n1; salaryrate = salary; } //function calculating the total Salaryrate public float calculate(int weekendhours) { return((40 * salaryrate) + (weekendhours * (salaryrate * 2))); } // get String representation of fulltime name public String toString(int hr) { return(name + " \t" + calculate(hr)); } /*public static void main(String[] args) { Fulltime f1 = new Fulltime("Jessica" , 100); System.out.println(f1.toString(50)); }*/ }// end class Part time worker subclass // Parttime.java public final class Parttime extends Worker { private float salary; private int hours; //default constructor public Parttime() { name = " "; salaryrate = 0.0f; } // parameterised constructors for class Parttime public Parttime (String n1, float salary) { name = n1; salaryrate = salary; } public float calculate(int x) { return 0; } //get parttimeworker pay public float calculate(int weekhours, int weekendhours) { if (weekhours < 40) return((float)((weekhours * salaryrate) + (weekendhours * (salaryrate * 1.5)))); else return((float) (((weekhours - 40) * 1.5) + (weekhours * salaryrate) + (weekendhours * (salaryrate * 1.5))) ); } public String toString(int hr1, int hr2 ) { return(name + " \t" + calculate(hr1, hr2)); } } Textfile from where application will read data of workers F Sandra Blues 9 7 6 8 7#5#2 ; F James Keys 8 6 7 9 5#6#4 ; F Bobby Louis 6 7 9 7 8#3#4 ; P Jessica Burns 8 4 6 2 3#6#5 P Nelly Oneil 5 6 4 3 9#2#2 F Erik Stifler 7 7 8 7 9#5#3 ; P Tracy Sterling 4 6 3 5 5#5#2 F John Tucker 10 8 9 7 8#5#4 ; F Kate Spencer 9 6 8 7 8#6#2 ; __________________________________________________ ______________ I now have to develop the Main application which will open the text file and read it. And will make the program run. But don’t know how to do it. Could anyone plz check my above coding if is gud and help me out with the Main application. Thnks again, Babe. |
| |
April 6th, 2007, 03:05 PM
|
#2 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,242
|
I'm a business programmer, COBOL, SQL/SQR etc. I know C++ quite well and JAVA is pretty close. Your worker class and methods "look" ok.
I would have written "main" first; but that's just the way I code. I always call it my "driver" and then branch-out from there. It's just easier that way. I then code "skeleton" methods/functions off that....for example:
"got first record"
"in calculate part time pay"
"data is P Jessica Burns 8 4 6 2 3#6#5"
Then it's just a matter of doing the math on the data.
You'll get it; your code seems clean enough from your post! GOOD LUCK
Search the net of get a book on JAVA and I'm sure it'll guide you in opening and processing files. |
| |
April 8th, 2007, 11:50 PM
|
#3 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
| Code: public float calculate(int weekhours, int weekendhours)
{
if (weekhours < 40)
return((float)((weekhours * salaryrate) + (weekendhours * (salaryrate * 1.5))));
else
return((float) (((weekhours - 40) * 1.5) + (weekhours * salaryrate) + (weekendhours * (salaryrate * 1.5))) );
}
this method is wrong..in particular in your else statement. Remember if they go over 40 (for example 50) hours....you pay 40 at regular and the rest at time and a half. you're first calculating anything over 40 and paying time and a half. Then your taking ALL their weekday hours and paying regular. assign numbers to your variable and do it out on paper. I think you'll quickly find your error
/edit. oh yea...your top if statement should be <=. It won't make a difference with the output...but as far as logic goes is they work 40 hours (40 = 40) you should still be picking the first option.
I code like you do babe. Start at the top and work my way down piece by piece until i get to the core (the main). Then go from there! Honestly make another class for file io so you can test that by itself...once that's done then implement the main method!!
/edit again. Just wanted to say much better question this time! This kinda stuff we don't mind helping with  . Good to see you didn't take what i said the wrong way and leave. Look forward to working on some fun programming problems with ya  Good luck with it and post back if you need more help/advice!
Last edited by sr71000 : April 9th, 2007 at 12:12 AM.
|
| | | Thread Tools | Search this Thread | | | |
Similar Threads | | Thread | Thread Starter | Forum | Replies | Last Post | | Average Salary | krotch | General Tech Discussion | 7 | February 14th, 2007 12:33 AM | | Negotiating a salary. | usslindstrom | IMO Community | 16 | August 21st, 2006 12:53 AM | | Salary Chart | maface | IMO Community | 21 | June 3rd, 2006 02:21 PM | | how to calculate binary? | Creatures | Webmastering and Programming | 15 | March 24th, 2003 03:04 PM | | Programmer Salary? | brandon184 | Certification and Education | 28 | September 3rd, 2002 04:33 AM | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |