Solved

Inheritance in Java

Posted on 2009-04-12
2
1,431 Views
Last Modified: 2012-05-06
I am working on lab for my computer science class, and I am almost done. The lab has to do with the inheritance principal in JAVA. The lab involves several types of employees, for example the commission employee takes on properties of the hourly employee (they both get paid hourly bu the commission employee gets paid a percentage of their total sales). When I try to compile, for the commission employee it gives me for their total pay as 0.0.(commission*totalSales + super.pay(); //derived from the "Hourly" class). It will make more sense if you run the program and see the assignment. The assignment is below.
______________________________________________________________________
The files Firm.java, Staff.java, StaffMember.java, Volunteer.java, Employee.java, Executive.java, and Hourly.java constitute a program that illustrates inheritance and polymorphism. In this exercise you will add one more employee type to the class hierarchy. The employee will be an hourly employee who also earns a commission on sales. Hence the class, which we'll name Commission, will be derived from the Hourly class.
NOTE: For this assignment, you should ONLY create/modify the Commission.java and Staff.java files! The other files are just support files for the rest of the program.


Create a new class named Commission with the following features:
It extends the Hourly class.

It has two instance variables (in addition to those that it inherits from the Hourly class): 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 (via super()) with the first 5 parameters and then use the 6th to set the commission rate.

One additional method is needed: public void addSales (double totalSales), which 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 value of hoursWorked back to 0  why not?).

The toString() method needs to call the toString() method of the parent class and add the total sales to the value returned by that method.
To test your class, update Staff.java as follows:

Increase the size of the array to 8.

Add two commissioned employees to the staffList array; 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 (0.2) and the other one earn $9.75 per hour and 15% commission (0.15).

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.
Compile and run the program. Make sure it is working properly.  

Lab12.zip
0
Comment
Question by:SpinnerAdam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 500 total points
ID: 24130442
In the Staff class, payday progresses as follows:

      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 ("-----------------------------------");
      }

Note that the *first* thing that occurs is the printing of the employee (the System.out.println call). This is accomplished by calling the toString method of the object being printed. The toString method for the Commission object is:

      public String toString()
      {               String result = super.toString();
             result += "\nTotal Sales: " + totalSales2;
             result += "\nCommision: " + commissionRate;  
             result += "\nTest: " + payment ;
          return result;
      }

So for test it is returning the value of payment. The payment variable is an instance member of the Commission class (declared at the top of the class). It is not initialized in the constructor, and so it will have a default value of 0. So when toString is called, that is what is printed.

The payment variable in Commission is used, and thus gets a value, in the pay method. However, as you can see from the loop in Staff.payday, the pay method is not called until *after* the toString method is called. Note that the "paid" output of the program correctly shows a number.
0
 
LVL 16

Expert Comment

by:imladris
ID: 24148805
Did that help?

If so, it is now time to close the question and grade the answer.

If not, perhaps a clarifying question would help.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

624 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