473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Constructors

I have a problem with the output of this program
this program consist of three classes
the fist one is

Expand|Select|Wrap|Line Numbers
  1. package c;
  2.  
  3. public class CommissionEmployee4
  4. {
  5.    private String firstName;
  6.    private String lastName;
  7.    private String socialSecurityNumber;
  8.    private double grossSales; // gross weekly sales
  9.    private double commissionRate; // commission percentage
  10.  
  11.    // five-argument constructor
  12.    public CommissionEmployee4( String first, String last, String ssn, 
  13.       double sales, double rate )
  14.    {
  15.       // implicit call to Object constructor occurs here
  16.       firstName = first;
  17.       lastName = last;
  18.       socialSecurityNumber = ssn;
  19.       setGrossSales( sales ); // validate and store gross sales
  20.       setCommissionRate( rate ); // validate and store commission rate
  21.  
  22.       System.out.printf( 
  23.          "\nCommissionEmployee4 constructor:\n%s\n", this );
  24.    } // end five-argument CommissionEmployee4 constructor
  25.  
  26.  
  27.  
  28.    // return first name
  29.    public String getFirstName()
  30.    {
  31.       return firstName;
  32.    } // end method getFirstName
  33.  
  34.    // return last name
  35.    public String getLastName()
  36.    {
  37.       return lastName;
  38.    } // end method getLastName
  39.  
  40.    // return social security number
  41.    public String getSocialSecurityNumber()
  42.    {
  43.       return socialSecurityNumber;
  44.    } // end method getSocialSecurityNumber
  45.  
  46.    // set gross sales amount
  47.    public void setGrossSales( double sales )
  48.    {
  49.       grossSales = ( sales < 0.0 ) ? 0.0 : sales;
  50.    } // end method setGrossSales
  51.  
  52.    // return gross sales amount
  53.    public double getGrossSales()
  54.    {
  55.       return grossSales;
  56.    } // end method getGrossSales
  57.  
  58.    // set commission rate
  59.    public void setCommissionRate( double rate )
  60.    {
  61.       commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
  62.    } // end method setCommissionRate
  63.  
  64.    // return commission rate
  65.    public double getCommissionRate()
  66.    {
  67.       return commissionRate;
  68.    } // end method getCommissionRate
  69.  
  70.  
  71.    // return String representation of CommissionEmployee4 object
  72.    public String toString()
  73.    {
  74.       return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", 
  75.          "commission employee", getFirstName(), getLastName(), 
  76.          "social security number", getSocialSecurityNumber(), 
  77.          "gross sales", getGrossSales(), 
  78.          "commission rate", getCommissionRate() );
  79.    } // end method toString
  80. } // end class CommissionEmployee4

the second class is inherited from the fist and inside its constructor ,explicitly call the construcror of the super class

Expand|Select|Wrap|Line Numbers
  1. package c;
  2.  
  3. public class BasePlusCommissionEmployee5 extends CommissionEmployee4
  4. {
  5.    private double baseSalary; // base salary per week
  6.  
  7.    // six-argument constructor
  8.    public BasePlusCommissionEmployee5( String first, String last, 
  9.       String ssn, double sales, double rate, double salary )
  10.    {
  11.       super( first, last, ssn, sales, rate );
  12.       setBaseSalary( salary ); // validate and store base salary
  13.  
  14.       System.out.printf( 
  15.          "\nBasePlusCommissionEmployee5 constructor:\n%s\n", this );
  16.    } // end six-argument BasePlusCommissionEmployee5 constructor
  17.  
  18.    // set base salary
  19.    public void setBaseSalary( double salary )
  20.    {
  21.       baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
  22.    } // end method setBaseSalary
  23.  
  24.    // return base salary
  25.    public double getBaseSalary()
  26.    {
  27.       return baseSalary;
  28.    } // end method getBaseSalary
  29.  
  30.  
  31.    // return String representation of BasePlusCommissionEmployee5
  32.    public String toString()
  33.    {
  34.       return String.format( "%s %s\n%s: %.2f", "base-salaried", 
  35.          super.toString(), "base salary", getBaseSalary() );
  36.    } // end method toString
  37. } // end class BasePlusCommissionEmployee5
the third class is that contain the main method and print and show how the contructors of the subclass call the constructor of the superclass

Expand|Select|Wrap|Line Numbers
  1. package c;
  2.  
  3. public class ConstructorTest {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         CommissionEmployee4 employee1 = new CommissionEmployee4( 
  8.          "Bob", "Lewis", "333-33-3333", 5000, .04 );
  9.  
  10.       System.out.println();
  11.       BasePlusCommissionEmployee5 employee2 = 
  12.          new BasePlusCommissionEmployee5( 
  13.          "Lisa", "Jones", "555-55-5555", 2000, .06, 800 );
  14.  
  15.       System.out.println();
  16.       BasePlusCommissionEmployee5 employee3 = 
  17.          new BasePlusCommissionEmployee5( 
  18.          "Mark", "Sands", "888-88-8888", 8000, .15, 2000 );
  19.     }
  20.  
  21. }
But the problem with me is abou this output

**************************the output*****************************
CommissionEmployee4 constructor:
commission employee: Bob Lewis
social security number: 333-33-3333
gross sales: 5000.00
commission rate: 0.04


CommissionEmployee4 constructor:
base-salaried commission employee: Lisa Jones
social security number: 555-55-5555
gross sales: 2000.00
commission rate: 0.06
base salary: 0.00

BasePlusCommissionEmployee5 constructor:
base-salaried commission employee: Lisa Jones
social security number: 555-55-5555
gross sales: 2000.00
commission rate: 0.06
base salary: 800.00


CommissionEmployee4 constructor:
base-salaried commission employee: Mark Sands
social security number: 888-88-8888
gross sales: 8000.00
commission rate: 0.15
base salary: 0.00

BasePlusCommissionEmployee5 constructor:
base-salaried commission employee: Mark Sands
social security number: 888-88-8888
gross sales: 8000.00
commission rate: 0.15
base salary: 2000.00
************************************************** **************
why the object employee2 print this statment---> (base salary: 0.00)
i know that when we create the employee2 object:first it call the CommissionEmployee4 constructor so it prints its values (i.e. "Lisa", "Jones", "555-55-5555", 2000, .06, 800 ) so why it print the base salary although there isn't in the CommissionEmployee4 constructor base salary
Mar 7 '07 #1
1 1474
horace1
1,510 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. public class CommissionEmployee4
  2. {
  3.    private String firstName;
  4.    private String lastName;
  5.    private String socialSecurityNumber;
  6.    private double grossSales; // gross weekly sales
  7.    private double commissionRate; // commission percentage
  8.  
  9.    // five-argument constructor
  10.    public CommissionEmployee4( String first, String last, String ssn, 
  11.       double sales, double rate )
  12.    {
  13.       // implicit call to Object constructor occurs here
  14.       firstName = first;
  15.       lastName = last;
  16.       socialSecurityNumber = ssn;
  17.       setGrossSales( sales ); // validate and store gross sales
  18.       setCommissionRate( rate ); // validate and store commission rate
  19.  
  20.       System.out.printf( 
  21.          "\nCommissionEmployee4 constructor:\n%s\n", this );
  22.    } // end five-argument CommissionEmployee4 constructor
  23.  
the 'this' in System.out.printf() calls the toString() in BasePlusCommissionEmployee5 hence the extra output, i.e. this refers to the current class instance which in this case is a BasePlusCommissionEmployee5 object.

change it to call toString() directly, e.g.
Expand|Select|Wrap|Line Numbers
  1.  
  2.    // five-argument constructor
  3.    public CommissionEmployee4( String first, String last, String ssn, 
  4.       double sales, double rate )
  5.    {
  6.       // implicit call to Object constructor occurs here
  7.       firstName = first;
  8.       lastName = last;
  9.       socialSecurityNumber = ssn;
  10.       setGrossSales( sales ); // validate and store gross sales
  11.       setCommissionRate( rate ); // validate and store commission rate
  12.  
  13.       System.out.printf( 
  14.          "\nCommissionEmployee4 constructor:\n%s\n", toString() );
  15.    } // end five-argument CommissionEmployee4 constructor
  16.  
  17.  
will call toString() in CommissionEmployee4
Mar 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.