Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
Like Tree1Likes

Thread: What's wrong with my constructor?

  1. #21
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: What's wrong with my constructor?

    I have explained what the problem with the constructor is multiple times. I don't feel like repeating myself!

    A single = is assignment and not comparison (equals to).

  2. #22
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by Junky View Post
    I have explained what the problem with the constructor is multiple times. I don't feel like repeating myself!
    NO! Look closer. The constructor I speak of is NOT in the Employee class, it's in the child ProductionWorker class. I shouldn't be getting that error with my constructor IN THE PRODUCTION WORKER CLASS, because I already included the extends Employee in my class declaration. It should just be inheriting "String Name", "String hireDate", and " int employeeNum" from the Employee class, NOT complaining about them!

    So again, why won't it let me declare and instantiate the ProductionWorker fields (int Shift; double payRate;) IN THE FRICKEN PRODUCTION WORKER CLASS WHERE THEY BELONG?! And what does the statement, "constructor Employee in class Employee cannot be applied to given types" mean? Here's the section of code with more comments to hopefully better clarify what my troubles are:

    Java Code:
    /*
     * This class inherits from the Employee class.  
     */
    package employment;
    
    
    public class ProductionWorker extends Employee{
        
        int Shift;
        double payRate; 
      
        
        public ProductionWorker(int shift, double pay)
        {//constructor Employee in class Employee cannot be applied to given types;
         //required: String,String,int
         //found: no arguments
         //reason: actual and formal argument lists differ in length
            Shift = shift;
            payRate = pay;
        }   
        
        //Junky, just to point them out for you, you do remember these from the 
        //Employee Class, don't you?:
        //String Name;
        //String hireDate;
        //int employeeNum;
        
        //See the order of the pattern (String,String,int)? Notice how it's similar 
        //to the error comment, "required: String,String,int; found: no arguments"?
        
        //How? the arguments are there: (int shift, double pay)

  3. #23
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: What's wrong with my constructor?

    Yes but ProductionWorker extends Employee and Employee has a constructor which requires three parameters; you are not providing those three parameters to the Employee constructor in your ProductionWorker constructor through the super keyword.

    Ex:

    Java Code:
    public abstract class TheSuperClass {
    
      private String name;
    
      public TheSuperClass(String name){
        this.name  = name;
      }
    }
    
    
    public class TheChildClass extends TheSuperClass {
    
       private int personnelNo;
    
       public TheChildClass(String name, int personnelNo){
         super(name);
         this.personnelNo = peronnelNo;
       }
    }
    And THAT is what Junky has been trying to tell you; you just won't listen.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #24
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by SamJava_the_Hut View Post
    NO! Look closer. The constructor I speak of is NOT in the Employee class, it's in the child ProductionWorker class. I shouldn't be getting that error with my constructor IN THE PRODUCTION WORKER CLASS, because I already included the extends Employee in my class declaration. It should just be inheriting "String Name", "String hireDate", and " int employeeNum" from the Employee class, NOT complaining about them!
    Yes, it's inheriting them, but, since Employee has only the one constructor, you need to provide those values.
    It's not complaining about those values, it's complaining because you haven't passed those values down to the Employee parent class via a super() call.
    gimbal2's example should point you in the right direction.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #25
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by gimbal2 View Post
    Yes but ProductionWorker extends Employee and Employee has a constructor which requires three parameters; you are not providing those three parameters to the Employee constructor in your ProductionWorker constructor through the super keyword.
    Thanks. That got rid of most of the red, but there are still a couple of ERROR's:

    Java Code:
    /*
     * This class inherits from the Employee class.  
     */
    package employment;
    
    import java.util.jar.Attributes;
    
    
    public class ProductionWorker extends Employee{
        
        //boolean Shift;//either shift 1 or 2
        int Shift;
        double payRate;
        //Later on, don't forget to declare a variable here with current date and 
        //time to calculate how long an employee has been with the company.  
        
        public ProductionWorker(String name, String aHireDate, int empNum, 
                                int shift, double pay)
        {//ERROR:   
         //constructor Employee in class Employee cannot be applied to given types;
         //required: String,String,int
         //found: no arguments
         //reason: actual and formal argument lists differ in length
            
         //I did what you told me to do right here:         
            super.setName(Name);
            super.setHireDate(hireDate);
            super.setEmployeeNum(employeeNum);
            Shift = shift;
            payRate = pay;
            //this.Shift = shift;
            //this.payRate = pay;
            
            //So why do I still have that same error message?! (Keep going. There's
            //still one more error in the getPayRate method at the bottom of this
            //class).
        }   
           
        
        //The setShift method accepts an argument
        //that is stored in the Shift field.    
    
       public void setShift(int shift)
       {
          Shift = shift;
       }
       
       
       //The getShift method returns the value
        //stored in the Shift field.    
    
       public int getShift()
       {
          return Shift;
       }
    
       
        //The setPayRate method accepts an argument
        //that is stored in the payRate field.    
    
       public void setPayRate(double pay)
       {
          payRate = pay;
       }
    
       //START OF OLD GETPAYRATE METHOD
        //The getPayRate method returns the value
        //stored in the payRate field.    
    
       //public double getPayRate()
       //{
          //return payRate;
       //}
       //END OF OLD GETPAYRATE METHOD
       
       
       //The getPayRate method returns an hourly pay  
       //based on which shift is selected by keyboard
       //input in the ProductionWorkerDemo class:
       
       public double getPayRate()
       {
           int Shift;
           
           //Shift 1 = day shift
           //Shift 2 = night shift
           
           if(Shift == 1)//ERROR: variable Shift might not have been initialized                            
           {
               payRate = 8.00;
           }
               
           else if(Shift == 2)
           {
               payRate = 10.00;
           }           
                       
          return payRate;
       }
        
    }//end of ProductionWorker class

  6. #26
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by gimbal2 View Post
    Yes but ProductionWorker extends Employee and Employee has a constructor which requires three parameters; you are not providing those three parameters to the Employee constructor in your ProductionWorker constructor through the super keyword.

    Ex:

    Java Code:
    public abstract class TheSuperClass {
    
      private String name;
    
      public TheSuperClass(String name){
        this.name  = name;
      }
    }
    
    
    public class TheChildClass extends TheSuperClass {
    
       private int personnelNo;
    
       public TheChildClass(String name, int personnelNo){
         super(name);
         this.personnelNo = peronnelNo;
       }
    }
    And THAT is what Junky has been trying to tell you; you just won't listen.
    Well, no one mentioned the super key word so far in this thread. If there is a God of syntax, he hates me. Please bare with me. I tried what you said, and here's my updated code (Employee and ProductionWorker classes):

    Employee (super) class:
    Java Code:
    /*
     * The workday is divided into two shifts: Day and Night. The shift field will 
     * be an integer value representing the shift that the employee works (Day is 
     * shift1, and Night is shift2). 
     */
    package employment;
    //import java.util.Calendar;
    import org.joda.time.DateTime;
    
    
    public class Employee {
        
        String Name;
        String hireDate;
        int employeeNum;
    
        //The following constructor accepts arguments for the employee's name,
        //hire date, and id number: 
        public Employee(String name, String aHireDate, int empNum)
            {
                this.Name = name;
                this.hireDate = aHireDate;
                this.employeeNum = empNum;
            }
            
            //Write one or more constructors and the appropriate accessor and 
            //mutator methods for the class. (Note that "accessor methods" are 
            //getters, and "mutator methods" are setters):
    
       
        //The setName method accepts an argument
        //that is stored in the Name field. 
        
       public void setName(String name)//error: "illegal start of expression"
       {
          Name = name;
       }
       
       
        //The setHireDate method accepts an argument
        //that is stored in the Name field.     
    
       public void setHireDate(String aHireDate)
       {
          hireDate = aHireDate;
       }
    
       
        //The setEmployeeNum method accepts an argument
        //that is stored in the payRate field.    
    
       public void setEmployeeNum(int empNum)
       {
          employeeNum = empNum;
       }
       
       
       //The getName method returns the value
       //stored in the Name field.   
    
       public String getName()
       {
          return Name;
       }   
    
       
        //The getPayRate method returns the value
        //stored in the payRate field.    
    
       public String getHireDate()
       {
          return hireDate;
       }
       
       
        //The getPayRate method returns the value
        //stored in the payRate field.    
       
       public int getEmployeeNum()
       {
          return employeeNum;
       }  
    
    }//end of Employee class (the super class)
    ProductionWorker class:

    Java Code:
    /*
     * This class inherits from the Employee class.  
     */
    package employment;
    
    import java.util.jar.Attributes;
    import org.joda.time.DateTime;
    
    public class ProductionWorker extends Employee{
        
        //boolean Shift;//either shift 1 or 2
        int Shift = 1;//I'm initializing it here, but it's still complaining in my
                      //getPayRate method further down.
        double payRate;
        //Later on, don't forget to declare a variable here with current date and 
        //time to calculate how long an employee has been with the company.  
        
        public ProductionWorker(String name, String aHireDate, int empNum, 
                                int shift, double pay)
        {//ERROR:   
         //constructor Employee in class Employee cannot be applied to given types;
         //required: String,String,int
         //found: no arguments
         //reason: actual and formal argument lists differ in length
            
         //I did what you told me to do right here:         
            super.setName(Name);//this way got rid of red
            super.setHireDate(hireDate);//this way got rid of red
            //super.setEmployeeNum(employeeNum);//this way got rid of red
            super.(employeeNum);//<identifier> expected 
            //(his way of super. syntax did not get rid of red) 
            Shift = shift;
            payRate = pay;
            //this.Shift = shift;
            //this.payRate = pay;
            
            //So why do I still have that same error message?! (
        }   
           
        
        //The setShift method accepts an argument
        //that is stored in the Shift field.    
    
       public void setShift(int shift)
       {
          Shift = shift;
       }
       
       
       //The getShift method returns the value
        //stored in the Shift field.    
    
       public int getShift()
       {
          return Shift;
       }
    
       
        //The setPayRate method accepts an argument
        //that is stored in the payRate field.    
    
       public void setPayRate(double pay)
       {
          payRate = pay;
       }
    
       //START OF OLD GETPAYRATE METHOD
        //The getPayRate method returns the value
        //stored in the payRate field.    
    
       //public double getPayRate()
       //{
          //return payRate;
       //}
       //END OF OLD GETPAYRATE METHOD
       
       
       //The getPayRate method returns an hourly pay  
       //based on which shift is selected by keyboard
       //input in the ProductionWorkerDemo class:
       
       public double getPayRate()
       {
           int Shift;
           
           //payRate is supposed to be the double
           //Shift is supposed to be the int
           //Shift 1 = day shift
           //Shift 2 = night shift
                  
           if(Shift == 1)//ERROR: variable Shift might not have been initialized                            
           {
               payRate = 8.00;
           }
               
           else if(Shift == 2)
           {
               payRate = 10.00;
           }           
                       
          return payRate;
       }
        
    }//end of ProductionWorker class
    Why doesn't your super keyword syntax work in my code?

  7. #27
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by SamJava_the_Hut View Post
    Well, no one mentioned the super key word so far in this thread.
    Reply #7!

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: What's wrong with my constructor?

    The Oracle tutorials cover this early on, in the Classes section of Learning The Java Language.
    The example on that page shows how MountainBike has to call the constructor for Bicycle, its parent class.
    This is described in the Inheritance section.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #29
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by SamJava_the_Hut View Post
    Why doesn't your super keyword syntax work in my code?
    Because you're not listening again. Try stopping to blunder ahead for a moment and actually taking the time to read and study the material.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #30
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: What's wrong with my constructor?

    Thank you. That Bicycle class really helped

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 10
    Last Post: 01-11-2013, 08:16 PM
  2. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  3. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  4. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  5. Replies: 0
    Last Post: 12-19-2007, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •