Constructors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • analoveu
    New Member
    • Jan 2007
    • 36

    Constructors

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

    Code:
    package c;
    
    public class CommissionEmployee4
    {
       private String firstName;
       private String lastName;
       private String socialSecurityNumber;
       private double grossSales; // gross weekly sales
       private double commissionRate; // commission percentage
    
       // five-argument constructor
       public CommissionEmployee4( String first, String last, String ssn, 
          double sales, double rate )
       {
          // implicit call to Object constructor occurs here
          firstName = first;
          lastName = last;
          socialSecurityNumber = ssn;
          setGrossSales( sales ); // validate and store gross sales
          setCommissionRate( rate ); // validate and store commission rate
    
          System.out.printf( 
             "\nCommissionEmployee4 constructor:\n%s\n", this );
       } // end five-argument CommissionEmployee4 constructor
    
    
    
       // return first name
       public String getFirstName()
       {
          return firstName;
       } // end method getFirstName
    
       // return last name
       public String getLastName()
       {
          return lastName;
       } // end method getLastName
    
       // return social security number
       public String getSocialSecurityNumber()
       {
          return socialSecurityNumber;
       } // end method getSocialSecurityNumber
    
       // set gross sales amount
       public void setGrossSales( double sales )
       {
          grossSales = ( sales < 0.0 ) ? 0.0 : sales;
       } // end method setGrossSales
    
       // return gross sales amount
       public double getGrossSales()
       {
          return grossSales;
       } // end method getGrossSales
       
       // set commission rate
       public void setCommissionRate( double rate )
       {
          commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
       } // end method setCommissionRate
    
       // return commission rate
       public double getCommissionRate()
       {
          return commissionRate;
       } // end method getCommissionRate
    
      
       // return String representation of CommissionEmployee4 object
       public String toString()
       {
          return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", 
             "commission employee", getFirstName(), getLastName(), 
             "social security number", getSocialSecurityNumber(), 
             "gross sales", getGrossSales(), 
             "commission rate", getCommissionRate() );
       } // end method toString
    } // end class CommissionEmployee4

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

    Code:
    package c;
    
    public class BasePlusCommissionEmployee5 extends CommissionEmployee4
    {
       private double baseSalary; // base salary per week
    
       // six-argument constructor
       public BasePlusCommissionEmployee5( String first, String last, 
          String ssn, double sales, double rate, double salary )
       {
          super( first, last, ssn, sales, rate );
          setBaseSalary( salary ); // validate and store base salary
       
          System.out.printf( 
             "\nBasePlusCommissionEmployee5 constructor:\n%s\n", this );
       } // end six-argument BasePlusCommissionEmployee5 constructor
       
       // set base salary
       public void setBaseSalary( double salary )
       {
          baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
       } // end method setBaseSalary
    
       // return base salary
       public double getBaseSalary()
       {
          return baseSalary;
       } // end method getBaseSalary
    
    
       // return String representation of BasePlusCommissionEmployee5
       public String toString()
       {
          return String.format( "%s %s\n%s: %.2f", "base-salaried", 
             super.toString(), "base salary", getBaseSalary() );
       } // end method toString
    } // 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

    Code:
    package c;
    
    public class ConstructorTest {
    
        public static void main(String[] args) {
            
            CommissionEmployee4 employee1 = new CommissionEmployee4( 
             "Bob", "Lewis", "333-33-3333", 5000, .04 );
          
          System.out.println();
          BasePlusCommissionEmployee5 employee2 = 
             new BasePlusCommissionEmployee5( 
             "Lisa", "Jones", "555-55-5555", 2000, .06, 800 );
          
          System.out.println();
          BasePlusCommissionEmployee5 employee3 = 
             new BasePlusCommissionEmployee5( 
             "Mark", "Sands", "888-88-8888", 8000, .15, 2000 );
        }
        
    }
    But the problem with me is abou this output

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


    CommissionEmplo yee4 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

    BasePlusCommiss ionEmployee5 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


    CommissionEmplo yee4 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

    BasePlusCommiss ionEmployee5 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 CommissionEmplo yee4 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 CommissionEmplo yee4 constructor base salary
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Code:
    public class CommissionEmployee4
    {
       private String firstName;
       private String lastName;
       private String socialSecurityNumber;
       private double grossSales; // gross weekly sales
       private double commissionRate; // commission percentage
    
       // five-argument constructor
       public CommissionEmployee4( String first, String last, String ssn, 
          double sales, double rate )
       {
          // implicit call to Object constructor occurs here
          firstName = first;
          lastName = last;
          socialSecurityNumber = ssn;
          setGrossSales( sales ); // validate and store gross sales
          setCommissionRate( rate ); // validate and store commission rate
    
          System.out.printf( 
             "\nCommissionEmployee4 constructor:\n%s\n", this );
       } // end five-argument CommissionEmployee4 constructor
    the 'this' in System.out.prin tf() calls the toString() in BasePlusCommiss ionEmployee5 hence the extra output, i.e. this refers to the current class instance which in this case is a BasePlusCommiss ionEmployee5 object.

    change it to call toString() directly, e.g.
    Code:
       // five-argument constructor
       public CommissionEmployee4( String first, String last, String ssn, 
          double sales, double rate )
       {
          // implicit call to Object constructor occurs here
          firstName = first;
          lastName = last;
          socialSecurityNumber = ssn;
          setGrossSales( sales ); // validate and store gross sales
          setCommissionRate( rate ); // validate and store commission rate
    
          System.out.printf( 
             "\nCommissionEmployee4 constructor:\n%s\n", toString() );
       } // end five-argument CommissionEmployee4 constructor
    will call toString() in CommissionEmplo yee4

    Comment

    Working...