Page 1 of 2 12 LastLast
Results 1 to 20 of 30
Like Tree1Likes

Thread: What's wrong with my constructor?

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

    Default What's wrong with my constructor?

    I'm having lots of problems setting up my constructor. The error for line 31 says, "illegal start of expression"; the error for lines 33 thru 35 say "cannot find symbol" (which I don't understand, because they're being declared in the super class, a super class is the main class, so why is it yelling at me?); and see my comment right after the class's final closing curly brace for the other error. Here's the code:

    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). This java project will be a modification of 
     * Programming Challenge 1 of Chapter 9 (Employee and ProductionWorker Classes).
     */
    package employment;
    //import java.util.Calendar;
    //import java.text.DateFormat;
    import org.joda.time.DateTime;
    
    
    public class Employee {
    
        public static void main(String[] args) {
            String Name;
            String hireDate;
            int employeeNum;
            //declare a variable here with current date and time to calculate how
            //long an employee has been with the company
            
            //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 following constructor accepts arguments for the employee's name,
             //hire date, and id number:
                    
            
            public Employee(String aName, String aHireDate, int aEmployeeNum)
            {
                Name = aName;
                hireDate = aHireDate;
                employeeNum = aEmployeeNum;
            }
            
            //DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        
        }//end of main method
    
    }//end of public class Employee; error: "class, interface, or enum expected"

    Are constructors even allowed in classes with a main method?
    Last edited by SamJava_the_Hut; 09-18-2013 at 02:17 AM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What's wrong with my constructor?

    Are constructors even allowed in classes with a main method?
    Yes! All normal classes have constructors whether you write one explicitly or not.

    Your problem on line 31 is because you tried to put a constructor inside a method - which is not allowed.

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

    Default Re: What's wrong with my constructor?

    The programming challenge directions in my text book also say to add a child class, "ProductionWorker", which will contain the employee's shift and hourly pay rate. I'm getting red at the opening curly brace of my ProductionWorker constructor, and I included a commented error message right in front of it. Here is my code so far:

    Java Code:
    /*
     * This is a child class of the Employee.java class. It's responsible for 
     * holding information on an employee's shift and hourly pay rate. 
     */
    package employment;
    
    
    public class ProductionWorker extends Employee{
        
        int Shift;
        double payRate;
        
        //3 vars from Employee.java super class:
        String Name;
        String hireDate;
        int employeeNum;
        
        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;
        }
    Does that mean I'm supposed to override the Employee constructor of the Employee super class?
    Last edited by SamJava_the_Hut; 09-18-2013 at 02:55 AM.

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

    Default Re: What's wrong with my constructor?

    All constructors (except in Object class) will call the constructor in the parent class. If you do not explicitly call a constructor in the parent class the compiler will call the default (no-args) constructor for you. Does your Employee class have a default constructor?

  5. #5
    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
    Does your Employee class have a default constructor?
    No, it doesn't. I haven't tried compiling the Employee class yet, because it still isn't really ready for that (and even if it was, I was strongly thinking it would be better if I just moved my main method to a new class, "ProductionWorkerDemo", that can demonstrate what's inside of both the Employee and ProductionWorker classes).

    Now, about that error comment from the ProductionWorker class:

    Java Code:
    /*
     * This is a child class of the Employee.java class. It's responsible for 
     * holding information on an employee's shift and hourly pay rate. 
     */
    package employment;
    
    
    public class ProductionWorker extends Employee{
        
        int Shift;
        double payRate;
        
        //3 vars from Employee.java super class:
        String Name;
        String hireDate;
        int employeeNum;
        
        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;
        }
    Judging by the pattern "String, String, int", I can only imagine it's complaining about not detecting the 3 fields from the Employee class:

    Java Code:
    package employment;
    //import java.util.Calendar;
    import org.joda.time.DateTime;
    
    /**
     *
     * @author speterson86
     */
    public class Employee {
    
        /**
         * @param args the command line arguments
         */
        //public static void main(String[] args) {//will soon move main method to 
                                                  //ProductionWorkerDemo class
            String Name;
            String hireDate;
            int employeeNum;
            //declare a variable here with current date and time to calculate how
            //long an employee has been with the company
            
            //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 following constructor accepts arguments for the employee's name,
             //hire date, and id number:                 
            
            public Employee(String aName, String aHireDate, int aEmployeeNum)
            {
                Name = aName;
                hireDate = aHireDate;
                employeeNum = aEmployeeNum;
            }        
        
        //}//end of main method
    
    }//end of Employee class (the super class)
    Am I wrong?

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

    Default Re: What's wrong with my constructor?

    Huh?

    How can you possibly write a child class before the parent class?

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

    Default Re: What's wrong with my constructor?

    I told you what the problem was. Your constructor is essentially this:
    Java Code:
     public ProductionWorker(int shift, double pay) {
            super(); // implicitly inserted for you by the compiler
            Shift = shift;
            payRate = pay;
        }
    Since the Employee class does not have a default constructor that is why you get the error message. To fix it you need to add the default constructor or call the only constructor the Employee class does have yourself.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What's wrong with my constructor?

    I haven't tried compiling the Employee class yet, because it still isn't really ready for that
    That is absolutely the wrong way to code. You should be compiling constantly - every couple of changes. If you don't, then you will have a mountain of errors by the time you do get around to compiling, and likely you will have no idea where many of them are coming from.

    The compiler will point out when you've written invalid syntax, but only if you let it. If you compile after every new statement or block, then you will know if your syntax was correct. This method would have pointed out some of your errors the moment they happened rather than later.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by quad64bit View Post
    That is absolutely the wrong way to code. You should be compiling constantly - every couple of changes. If you don't, then you will have a mountain of errors by the time you do get around to compiling, and likely you will have no idea where many of them are coming from.
    I went to Poly with a chap who worked like that.
    Obviously, this was the pre-auto compile IDE days, but compiling was hardly an arduous task.
    He would hammer out a thousand lines of code and then go through a compile-and-fix phase, removing compilation errors.

    I have no idea if he ever actually got anything to work, but I presume he must have done since he got his degree.
    I dread to think what some of it must have looked like, though. Maintainability would not have been terribly high.
    quad64bit likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by quad64bit View Post
    you tried to put a constructor inside a method - which is not allowed.
    Well, I declared my fields and custom constructor before the main method (I also compiled my Employee class with errors, so if the default constructor does now exist, I guess it's invisible?), but now I'm getting a couple of other errors (see commented error descriptions in my updated code):

    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). This java project will be a modification of 
     * Programming Challenge 1 of Chapter 9 (Employee and ProductionWorker Classes).
     */
    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)
            {
                Name = name;
                hireDate = aHireDate;
                employeeNum = empNum;
            }
        
        
        public static void main(String[] args) {//will soon move main method to 
                                                  //ProductionWorkerDemo class
            String Name;
            String hireDate;
            int employeeNum;
            //declare a variable here with current date and time to calculate how
            //long an employee has been with the company
            
            //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 main method
    
    }//end of Employee class (the super class)//error: "class, interface, or enum expected"
    
    //Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
    	//at employment.Employee.main(Employee.java:45)
    //Java Resu/lt: 1

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,702
    Rep Power
    5

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by Tolls View Post
    I went to Poly with a chap who worked like that.
    Obviously, this was the pre-auto compile IDE days, but compiling was hardly an arduous task.
    He would hammer out a thousand lines of code and then go through a compile-and-fix phase, removing compilation errors.
    To be honest, I was like that too pre-IDE days (especially when coding C/C++). Having to compile every few lines just takes you out of "the zone" where you can hammer out all the awesomeness that is floating around in your brain - it sucks in the beginning due to the mentioned mountain of errors but eventually you get really good at programming WITHOUT making mistakes. Call it programming boot camp.

    I thank the gods for incremental compilation in IDEs nowadays since I like more to program in a more relaxed state where I don't have to be on edge the whole time trying to scan for possible mistakes!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: What's wrong with my constructor?

    When I was in school or course I would write all my code and then compile. First, all my programs were on punch cards. Second, compilation cost "money" and if you exhausted your computer account, you had to wait until more could be added.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by jim829 View Post
    When I was in school or course I would write all my code and then compile. First, all my programs were on punch cards. Second, compilation cost "money" and if you exhausted your computer account, you had to wait until more could be added.
    So what's your point?

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

    Default Re: What's wrong with my constructor?

    The default constructor in the Employee class is not invisible. It does not exist. As soon as you write your own constructor the default constructor is not implicitly supplied. You have to write it yourself. Having said that I believe what you child class should be doing is calling the existing Employee constructor passing the required information. this is one of the main points of inheritance.

    As for the error, you have the setName method inside the main method. That's a no-no.

  15. #15
    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 believe what you child class should be doing is calling the existing Employee constructor passing the required information. this is one of the main points of inheritance. As for the error, you have the setName method inside the main method. That's a no-no.
    So what should my Employee class's main method be instantiating instead of 'the 3 fields declared and put in the constructor' before my main method? And where should my getter and setter methods be for those 3 fields if not inside the main method? (I haven't updated my Employee class code yet, so it is still the same as it most recently appears in the posts of this thread, just so you know so I don't waste space unnecessarily pasting my code here again in this post).

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

    Default Re: What's wrong with my constructor?

    The only time I mentioned the main method was to say you cannot have one method inside another. I did not mention anything about where and when you gather information or where and when you use that information. Where you put your main method and what you do in it is upto you.

    You have an Employee class that holds some information about a person. You extend that class with ProductionWorker that has all the info that Employee has but more. So why do you have name etc declared in both classes. Gather all the information and pass to the ProductionWorker constructor which then passes the relevant information to the Employee class constructor.

    And for gawds sake work on one class at a time. Finish the code compile it and remove all errors before moving onto the next. Your current approach is generating way too many errors which you cannot fix because it relies on something being fixed in the other class first.

  17. #17
    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
    Finish the code compile it and remove all errors before moving onto the next.
    Is it possible to compile a class (test run a file) without any output? My Employee class does not have any statements that produce output, only instantiations.

    And is it a bad idea to have a main method in more than one class per package?

  18. #18
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,702
    Rep Power
    5

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by SamJava_the_Hut View Post
    Is it possible to compile a class (test run a file) without any output?
    Output is very vague. What do you mean by "output" ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: What's wrong with my constructor?

    1. I would (as you suggest yourself in post 5) move the main() method out to its own home. It's getting in the way where it is.
    2. Do as suggested by Junky and finish the Employee class so it contains everything you need and it at least compiles. You don't have to run it or anything.
    3. Write your child class, possibly at the same time as dealing with how your main method is going to run in order to test it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: What's wrong with my constructor?

    Quote Originally Posted by Tolls View Post
    Write your child class, possibly at the same time as dealing with how your main method is going to run in order to test it.
    Wow! This is starting to make more sense. But I do have another error in the child "ProductionWorker" class. See the error message commented in the curly braces of the ProductionWorker constructor:

    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;
        }   
        
       
        //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 double getShift()
       {
          return Shift;
       }
    
       
        //The setPayRate method accepts an argument
        //that is stored in the payRate field.
        
    
       public void setPayRate(double pay)
       {
          payRate = pay;
       }
    
       
        //The getPayRate method returns the value
        //stored in the payRate field.    
    
       //public double getPayRate()
       //{
          //return payRate;
       //}
       
       
       //The getPayRate method returns an hourly pay  
       //based on which shift is worked:
       
       public double getPayRate()
       {
           double payRate;
           
           if(shift = 1)//shift 1 = day shift
               payRate = '8.00';
           else if(shift = 2)//shift 2 = night shift
               payRate = '10.00';
                       
          return payRate;
       }
        
    }//end of ProductionWorker class
    I don't understand what's going on with that. Also, I'm getting lots and lots of red problems from lines 67 to 77. Any ideas to why it hates my getPayRate method so much?
    Last edited by SamJava_the_Hut; 09-19-2013 at 10:04 PM.

Page 1 of 2 12 LastLast

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
  •