-1

In my Commission class i am trying to overwrite the pay method from the parent class (Hourly) to compute the pay for hours worked, how would i do this? My code for Commission and Hourly class which is the parent, is below, the overwriting takes place where i put the ????

public class Commission extends Hourly
{
  double total_sales;
  double commission_rate;

  public Commission(String name, String address, String phone,
                    String soc_sec_number, double rate,double commission_rate)
  {
    super(  name,   address,   phone,
                      soc_sec_number,   rate);
    // set commission rate
     commission_rate = 0.02;
  }

  public void addSales (double sales)
  {
    sales += total_sales;

    /*????   */super.pay
  }

}

and the Hourly class

// ********************************************************************
// Hourly.java       Java Foundations
//
// Represents an employee that gets paid by the hour
// ********************************************************************

public class Hourly extends Employee
{
  private int hours_worked;

  // -------------------------------------------------------------------------
  // Constructor: Sets up this hourly employee using the specified information
  // -------------------------------------------------------------------------
  public Hourly(String name, String address, String phone,
                String soc_sec_number, double rate)
  {
    super(name, address, phone, soc_sec_number, rate);
    hours_worked = 0;
  }

  // -----------------------------------------------------
  // Adds the specified number of hours to this employee's
  // accumulated hours
  // -----------------------------------------------------
  public void addHours(int more_hours)
  {
    hours_worked += more_hours;
  }

  // -----------------------------------------------------
  // Computes and returns the pay for this hourly employee
  // -----------------------------------------------------
  public double pay()
  {
    double payment = pay_rate * hours_worked;

    hours_worked = 0;
    return payment;
  }

  // ----------------------------------------------------------
  // Returns information about this hourly employee as a string
  // ----------------------------------------------------------
  public String toString()
  {
    return super.toString() + "\nCurrent hours: " + hours_worked;
  }
}
b.wood
  • 11
  • 1
  • Please define the desired behavior. – MikeCAT Apr 30 '16 at 13:54
  • Maybe you should also consider using BigDecimal instead of double. – Fildor Apr 30 '16 at 13:57
  • Your question states that you're "supposed to override the pay method" but in your code you do no such thing -- Why? Instead you're creating a new method, `addSales(...)` that you don't define at all for us -- again, why? – Hovercraft Full Of Eels Apr 30 '16 at 14:00
  • @MikeCAt what do you mean desired behavior??? The overridden pay method must call the pay method from the parent class to compute the pay for hours worked then add to that the pay from commission on sales(totals sales times the commission rate) The total sales should be reset to 0 after the payment is calculated. – b.wood Apr 30 '16 at 14:00
  • @HovercraftFullOfEels The addSales method adds the parameter to the instance variable representing total sales. How would you override the pay method in Commission from Hourly? – b.wood Apr 30 '16 at 14:03
  • @b.wood: I would start by in fact **overriding the method**. That means what I stated in my comment above -- giving the child class a method with the same signature. You will want to first study your notes a little on just what overriding means. – Hovercraft Full Of Eels Apr 30 '16 at 14:06
  • @HovercraftFullOfEels that link and marking this as a duplicate did not help at all. – b.wood Apr 30 '16 at 14:25
  • It helps the site by closing an obvious duplicate. The key here is you need to learn what overriding means, pure and simple, but the site does not need yet another question on this. – Hovercraft Full Of Eels Apr 30 '16 at 14:46

1 Answers1

0

To your class Commission you add the override of pay like this:

@Override
public double pay() {
    double payment = super.pay();   // call the method of the parent
    ....                            // add other code
}
Loris Securo
  • 7,538
  • 2
  • 17
  • 28
  • it is supposed to compute the pay for hours worked then add to that the pay from the commission on sales(total sales * commission rate) how would i do that? – b.wood Apr 30 '16 at 15:47