Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Please advise on this polymorphism assignment

SundaeSundae Member Posts: 12
I need help with an assignment - have got confused in the process of coding. I am a student and have this assignment to do. It is long, but let me explain what they want.

"Write a superclass Worker & subclasses HourlyWorker and SalariedWorker. Every worker has a name and a salary rate. Write a method computePay(int hours) that computes the weekly pay for every worker. An hourly worker gets paid the hourly wage for the actual number of hours worked, if hours are at most 40. If the hourly worker worked more than 40 hours, the excess is paid at time and a half. The salaried worker gets paid the hourly wage of 40 hours, no matter what the actual number of hours is. Write a static method that use polymorphism to compute the pay of any Worker. Supply a test program that test these classes and methods."

Would prefer if someone could email me on how to do this, and then I can show the code that I have done, and I seem to be totally LOST in this jungle of codes.

Your help will be greatly appreciated. Thank you

Comments

  • QueezyTheGreatQueezyTheGreat Member Posts: 70
    [size=1]
    : I need help with an assignment - have got confused in the process of coding. I am a student and have this assignment to do. It is long, but let me explain what they want.
    :
    : "Write a superclass Worker & subclasses HourlyWorker and SalariedWorker. Every worker has a name and a salary rate. Write a method computePay(int hours) that computes the weekly pay for every worker. An hourly worker gets paid the hourly wage for the actual number of hours worked, if hours are at most 40. If the hourly worker worked more than 40 hours, the excess is paid at time and a half. The salaried worker gets paid the hourly wage of 40 hours, no matter what the actual number of hours is. Write a static method that use polymorphism to compute the pay of any Worker. Supply a test program that test these classes and methods."
    :
    : Would prefer if someone could email me on how to do this, and then I can show the code that I have done, and I seem to be totally LOST in this jungle of codes.
    :
    : Your help will be greatly appreciated. Thank you
    [/size]

    Here is the source code for your polymorphism :
    [code]
    /**
    * The base class for HourlyWorker
    * and SalariedWorker.
    */
    public abstract class Worker{

    public static final double SALARYRATE;
    public static final int HOURS=40;
    protected String workerName;

    public static abstract double computePay(int hours);
    }

    /**
    * HourlyWorker subclass
    */
    public class HourlyWorkker
    extends Worker{

    public static final double SALARYRATE=1.0;
    public stativ final double OVERTIMERATE=1.5;
    public static final int HOURS=40;
    private String workerName;

    /**
    * Constructor that accepts the workers
    * name.
    */
    public HourlyWorker(String workerName){
    this.workerName=workerName;
    }

    /**
    *
    */
    public static double computePay(int hours){
    if(hours>HOURS){
    int excess=hours-HOUR;
    return (HOUR*SALARYRATE)+(excess*OVERTIMERATE);
    }
    return SALARYRATE*hours;
    }
    }



    /**
    *
    */
    public class SalariedWorker
    extends Worker{

    public static final double SALARYRATE=1.0;
    public static final int HOURS=40;
    private String workerName;

    /**
    * Constructor that accepts the workers
    * name.
    */
    public SalariedWorker(String workerName){
    this.workerName=workerName;
    }

    /**
    *
    */
    public static double computePay(int hours){
    return SALARYRATE*HOURS;
    }
    }
    /**
    * Tester class
    */
    public class WorkerTester{
    public static void main(){
    Worker worker1=new SalariedWorker("Worker 1");
    Worker worker2=new HourlyWorkker("Workwer 2");
    System.out.println(worker1.computePay(45));
    System.out.println(worker2.computePay(45));
    }
    }
    [/code]
    Just be aware that I am at school and I did not check for errors, but
    it should work.Hope that helps.:-)


    -------==[QTZ]==------
    -==[QueezyTheGreat]==-

  • SundaeSundae Member Posts: 12
    : Here is the source code for your polymorphism :
    : [code]
    : /**
    : * The base class for HourlyWorker
    : * and SalariedWorker.
    : */
    : public abstract class Worker{
    :
    : public static final double SALARYRATE;
    : public static final int HOURS=40;
    : protected String workerName;
    :
    : public static abstract double computePay(int hours);
    : }
    :
    : /**
    : * HourlyWorker subclass
    : */
    : public class HourlyWorkker
    : extends Worker{
    :
    : public static final double SALARYRATE=1.0;
    : public stativ final double OVERTIMERATE=1.5;
    : public static final int HOURS=40;
    : private String workerName;
    :
    : /**
    : * Constructor that accepts the workers
    : * name.
    : */
    : public HourlyWorker(String workerName){
    : this.workerName=workerName;
    : }
    :
    : /**
    : *
    : */
    : public static double computePay(int hours){
    : if(hours>HOURS){
    : int excess=hours-HOUR;
    : return (HOUR*SALARYRATE)+(excess*OVERTIMERATE);
    : }
    : return SALARYRATE*hours;
    : }
    : }
    :
    :
    :
    : /**
    : *
    : */
    : public class SalariedWorker
    : extends Worker{
    :
    : public static final double SALARYRATE=1.0;
    : public static final int HOURS=40;
    : private String workerName;
    :
    : /**
    : * Constructor that accepts the workers
    : * name.
    : */
    : public SalariedWorker(String workerName){
    : this.workerName=workerName;
    : }
    :
    : /**
    : *
    : */
    : public static double computePay(int hours){
    : return SALARYRATE*HOURS;
    : }
    : }
    : /**
    : * Tester class
    : */
    : public class WorkerTester{
    : public static void main(){
    : Worker worker1=new SalariedWorker("Worker 1");
    : Worker worker2=new HourlyWorkker("Workwer 2");
    : System.out.println(worker1.computePay(45));
    : System.out.println(worker2.computePay(45));
    : }
    : }
    : [/code]
    : Just be aware that I am at school and I did not check for errors, but
    : it should work.Hope that helps.:-)
    :
    :
    : -------==[QTZ]==------
    : -==[QueezyTheGreat]==-
    :
    : Thank you for your kind help, very much appreciated. It came out with lots of errors, but the point is I now understand how you break it up with methods and constructors. What I do not understand is this :
    "this.workerName=workerName;"
    What is it for or what does it do.
    Thank you.

    ~~~~Sundae~~~~

  • QueezyTheGreatQueezyTheGreat Member Posts: 70
    [size=1]
    : Thank you for your kind help, very much appreciated. It came out
    :with lots of errors, but the point is I now understand how you break
    :it up with methods and constructors. What I do not understand is this :
    : "this.workerName=workerName;"
    : What is it for or what does it do.
    : Thank you.
    :
    : ~~~~Sundae~~~~
    [/size]
    I`m very sorry about the errors I was really not thinking about the
    syntax, I was more woried by finishing the post so that you have some
    code to experiment with. Sorry again. Now about
    [b]this.workerName=workerName[/b] what this means is that because you
    have alredy definied a variable in your class the this.this.workerName
    is saying to use the class variable instead of the local scope variable
    which you defined in the method signmature [b]Worker(String workerName)[/b]
    So this statment is just saying assign the local scope variable to
    the class variable. Those this clear it up? If you still don`t get it
    I`ll gladly help you. And again sorry about the errors.


    -------==[QTZ]==------
    -==[QueezyTheGreat]==-

  • Shawn CarterShawn Carter Member Posts: 0

    ________ \ http://forcoder.org \ free video tutorials and ebooks about | Perl, Delphi, C#, Java, Go, Visual Basic .NET, Scratch, R, Ruby, C, Objective-C, Visual Basic, PHP, Swift, Python, Assembly, MATLAB, JavaScript, PL/SQL, C++ F#, FoxPro, Lisp, Scheme, Apex, Transact-SQL, Lua, Prolog, Dart, Kotlin, Alice, Clojure, ML, Crystal, Rust, Fortran, LabVIEW, D, Ada, VBScript, Awk, Hack, COBOL, Julia, Bash, Scala, Logo, SAS, ABAP, Erlang | _

Sign In or Register to comment.