?
Solved

How to write write this class

Posted on 2005-05-02
7
Medium Priority
?
482 Views
Last Modified: 2012-06-27
public class Hourly extends Employee
{
   private int hoursWorked;

 
   public Hourly (String eName, String eAddress, String ePhone,
                  String socSecNumber, double rate)
   {
      super (eName, eAddress, ePhone, socSecNumber, rate);

      hoursWorked = 0;
   }

    public void addHours (int moreHours)
   {
      hoursWorked += moreHours;
   }

   
   public double pay()
   {
      double payment = payRate * hoursWorked;

      hoursWorked = 0;

      return payment;
   }

 
   public String toString()
   {
      String result = super.toString();

      result += "\nCurrent hours: " + hoursWorked;

      return result;
   }
}



•      It extends the Hourly class.
•      It has two instance variables (in addition to those inherited): one is the total sales the employee has made (type double) and the second is the commission rate for the employee (the commission rate will be type double and will represent the percent (in decimal form) commission the employee earns on sales (so .2 would mean the employee earns 20% commission on sales)).
•      The constructor takes 6 parameters: the first 5 are the same as for Hourly (name, address, phone number, social security number, hourly pay rate) and the 6th is the commission rate for the employee. The constructor should call the constructor of the parent class with the first 5 parameters then use the 6th to set the commission rate.
•      One additional method is needed: public void addSales (double totalSales) that adds the parameter to the instance variable representing total sales.
•      The pay method must call the pay method of the parent class to compute the pay for hours worked then add to that the pay from commission on sales. (See the pay method in the Executive class.) The total sales should be set back to 0 (note: you don't need to set the hoursWorked back to 0—why not?).
•      The toString method needs to call the toString method of the parent class then add the total sales to that.

To test your class, update Staff.java as follows:

•      Increase the size of the array to 8.
•      Add two commissioned employees to the staffList—make up your own names, addresses, phone numbers and social security numbers. Have one of the employees earn $6.25 per hour and 20% commission and the other one earn $9.75 per hour and 15% commission.
•      For the first additional employee you added, put the hours worked at 35 and the total sales $400; for the second, put the hours at 40 and the sales at $950
0
Comment
Question by:Ansary100
7 Comments
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13908246
Hi Ansary100,
This looks like home work, is it?

Cheers,
Steve
0
 

Author Comment

by:Ansary100
ID: 13908294
this is not homework but I am interesting study java so I found this question at book java I hope know how answers this question to understand how Inhertance work

Thank you for helping me
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13908535
Hi Ansary100,

what problem are you having with it exactly? What have you tried so far?

Regards;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Ansary100
ID: 13908552


I am starting with this but I don't know what next

public class Commission  extends Hourly
{
     private double totalSale;
     private double commissionRate;



      public Commission (String eName, String eAddress, String ePhone,
                  String socSecNumber, double rate, double c ommission)
   {
      super (eName, eAddress, ePhone, socSecNumber, rate);

0
 

Accepted Solution

by:
mariozivic earned 1500 total points
ID: 13908652
public class Commission extends Hourly
{
    private double sales;
    private double commission;
   
    public Sales(String eName, String eAddress, String ePhone, String socSecNumber, double rate, double commission)
    {
        super(eName, eAddress, ePhone, socSecNumber, rate);
        this.commission = commission;
       
        sales = 0;
    }
   
    public void addSales(double totalSales)
    {
        sales += totalSales;
    }
   
    public double pay()
    {
        double payment = super.pay();
        payment += sales * comission;
       
        sales = 0;
       
        return payment;
    }
   
    public String toString()
    {
       String result = super.toString();

       result += "\nComission: " + commission;
       result += "\nCurrent sales: " + sales;

       return result;
    }
}

You don't need to set hoursWorked back to 0 because call to super.pay() will do that for you.

I hope this answered your question.
0
 

Author Comment

by:Ansary100
ID: 13908721
I start with this code and how should finish it thank you again

 Increase the size of the array to 8.
  Add two commissioned employees to the staffList—make up your own names, addresses, phone numbers and social security numbers. Have one of the employees earn $6.25 per hour and 20% commission and the other one earn $9.75 per hour and 15% commission.
•     For the first additional employee you added, put the hours worked at 35 and the total sales $400; for the second, put the hours at 40 and the sales at $950






public class Staff
{
   StaffMember[] staffList;

 
   public Staff ()
   {
      staffList = new StaffMember[6];

      staffList[0] = new Executive ("Sam", "123 Main Line",
         "555-0469", "123-45-6789", 2423.07);

      staffList[1] = new Employee ("Carla", "456 Off Line",
         "555-0101", "987-65-4321", 1246.15);
      staffList[2] = new Employee ("Woody", "789 Off Rocker",
         "555-0000", "010-20-3040", 1169.23);

      staffList[3] = new Hourly ("Diane", "678 Fifth Ave.",
         "555-0690", "958-47-3625", 10.55);

      staffList[4] = new Volunteer ("Norm", "987 Suds Blvd.",
         "555-8374");
      staffList[5] = new Volunteer ("Cliff", "321 Duds Lane",
         "555-7282");

      ((Executive)staffList[0]).awardBonus (500.00);

      ((Hourly)staffList[3]).addHours (40);
   }

   
   public void payday ()
   {
      double amount;

      for (int count=0; count < staffList.length; count++)
      {
         System.out.println (staffList[count]);

         amount = staffList[count].pay();  // polymorphic

         if (amount == 0.0)
            System.out.println ("Thanks!");
         else
            System.out.println ("Paid: " + amount);

         System.out.println ("-----------------------------------");
      }
   }
}
0
 

Expert Comment

by:mariozivic
ID: 13908960
Add the following code to the end of Staff constructor:

      StaffMember[] temp = new StaffMember[8];
      System.arraycopy(temp, 0, staffList, 0, 6);
      staffList = temp;
     
      staffList[6] = new Commission("Pero", "12 Palaca", "123-7282", 6.25, 0.2);
      staffList[7] = new Commission("Zdero", "13 Pravda", "721-2541", 9.75, 0.15);
     
      ((Commission)staffList[6]).addHours(35);
      ((Commission)staffList[6]).addSales(400);

      ((Commission)staffList[7]).addHours(40);
      ((Commission)staffList[7]).addSales(950);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question