Results 1 to 5 of 5
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Why do I get this error?

    I'm revising polymorphic arrays and I get an error. I dont know why this is. Anyway my array just processes different employee types (they each inherit from the abstract Employee class and have their own implementation of earnings() - I also make changes to a commission employee

    Java Code:
    public static void main(String[] args)
    		{
    			SalariedEmployee salariedEmployee = new SalariedEmployee("John", 35);
    			CommissionEmployee commissionEmployee = new CommissionEmployee("Haruka", 43, 0.5);
    			
    			Employee[] employees = new Employee[2];
    
    			employees[0] = salariedEmployee;
    			employees[1] = commissionEmployee;
    			
    			for( Employee currentEmployee : employees );
    			{
    				if( currentEmployee instanceof CommissionEmployee )
    				{
    					CommissionEmployee employee = (CommissionEmployee) currentEmployee;
    					
    					employee.setCommissionRate(0.8);
    				}
    				
    				System.out.println("Employee type: " + employees[currentEmployee].getClass().
    						getName() + " earns " + currentEmployee.earnings();
    			}
    		}
    Eclipse says "currentEmployee cannot be resolved to a variable" in the if statement, but havent I already declared it in the enhanced for loop? the stack trace is

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	currentEmployee cannot be resolved to a variable
    	currentEmployee cannot be resolved to a variable
    	currentEmployee cannot be resolved to a variable
    	currentEmployee cannot be resolved
    	Syntax error, insert ")" to complete Expression
    
    	at Workshop.main(Workshop.java:184)
    what ")"? I've looked over this and dont know what I'm missing
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  2. #2
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why do I get this error?

    I had an extra semi-colon... why does this happen? I looked it over beforehand and then realise only after I've posted. typical. ugh if I hadnt stopped java this syntax wouldnt have happened... I also see how I cant use the enhanced loop for printing the class name and stuff at the end I need a normal loop
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why do I get this error?

    This:
    Java Code:
                for( Employee currentEmployee : employees );
                {
                    if( currentEmployee instanceof CommissionEmployee )
                    {
                        CommissionEmployee employee = (CommissionEmployee) currentEmployee;
                         
                        employee.setCommissionRate(0.8);
                    }
                     
                    System.out.println("Employee type: " + employees[currentEmployee].getClass().
                            getName() + " earns " + currentEmployee.earnings();
                }
    Is equivalent to this:

    Java Code:
                for( Employee currentEmployee : employees )
                {
                    ; // this empty statement gets called in the loop.
                }
    
                // this block runs no matter what
                {
                    if( currentEmployee instanceof CommissionEmployee )
                    {
                        CommissionEmployee employee = (CommissionEmployee) currentEmployee;
                         
                        employee.setCommissionRate(0.8);
                    }
                     
                    System.out.println("Employee type: " + employees[currentEmployee].getClass().
                            getName() + " earns " + currentEmployee.earnings();
                }

  4. #4
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why do I get this error?

    Yes I see thank you, to make this thread somewhat more productive I do have a question I would like to ask of you.

    I'm implementing interface payable on Employee class.
    payable contains as abstact(obviously) method called getpayment

    now, because Employee class is abstract it doesnt provide an implementation (the method is only visable in payable and the subclasses of Employee.

    Is this how it should be? can I write

    public abstract double getpayment()

    in Employee class even know I dont need to (to make it clearer that this is being passed down) or do I just leave it out? what is best practice.
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Why do I get this error?

    Leave it out.
    Java doc would pick up the existence of the inherited method, and the compiler (or IDE) would pick up the lack of an implementation in any of the Employee subclasses. No need to fill up Employee with irrelevant stuff.

    Though I would call it Payable, and getPayment()...naming standards and all that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 3
    Last Post: 01-23-2009, 08:53 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
  •