Results 1 to 14 of 14

Thread: A small problem

  1. #1
    Imstuck is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default A small problem

    hey guys!

    I have a small problem, it's probably easy for alot of you but I'm kind of new to Java so I would like some help.

    I have class (call it 'worker') and this class has a method called "work".

    I have a second class in which i have an array variable stored with 'worker' object.
    However when i try to use the 'work' method on one of those objects i get the error: 'cannot find symbol - method work()'

    Can someone help me?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default Re: A small problem

    Does that method take parameters?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Imstuck is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: A small problem

    Worker class has a field named 'hours'

    the method is:

    public void work(int hours){
    this.hours += hours;
    }

    In the second class this is my constructor:

    public secondClass(){
    Staffmember[] members = new Staffmember[4];
    member[0] = new Worker("Jan", 1000);
    member[1] = new Worker("Jef", 25);


    this.member = member;

    member[0].work(10);
    member[1].werk(15);


    Staffmember is a superclass of worker. This contructor creates 2 workers (a subclass of staffmember) and puts them in a arraylist.
    I got it working till there.

    Now i try to use the work method on one of those workers but get the mentioned error.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default Re: A small problem

    All that the compiler knows is that the array elements are StaffMembers and Staffmembers don't have a work( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,425
    Rep Power
    20

    Default Re: A small problem

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Imstuck is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: A small problem

    JoSah yes that's most probably the problem, how do I change it though? don't i suggest it's a worker when i say member[0] = new Worker("Jan", 1000); ?

    Here is the full code (it's in dutch but you'll hopefully understand)

    Worker:
    Java Code:
     public class Arbeider extends Personeelslid{
    private int uurloon;
    private int aantalUren;
    public Arbeider(String naam, int uurloon){
    super(naam);
    this.uurloon = uurloon;
    aantalUren = 0;
    
    }
    
    public void werk(int uren){
    this.aantalUren += uren;
    }
    
    public int getLoon(){
    int loon = 0;
    loon = aantalUren * uurloon;
    return loon;
    }
    
    }
    Staffmembers:

    Java Code:
     public class Personeelslid{
        public String naam;
    
        public Personeelslid(String naam){
            this.naam = naam;
        }
    
        public String getNaam(){
            return naam;
        }
    
        public int getLoon(){
            return 0;
        }
    
    }
    Staff code:

    Java Code:
     public class Personeel{
    
        private Personeelslid[] leden;
    
        public Personeel(){
            Personeelslid[] leden = new Personeelslid[4];
            leden[0] = new Bediende("Jan", 1000);
            leden[1] = new Arbeider("Jef", 25);
            leden[2] = new Bediende("Jop", 1250);
            leden[3] = new Arbeider("Jeroen", 35);
    
            this.leden = leden;
            
            leden[1].werk(10);
            leden[3].werk(15);
            
    
        }
    
        public void betaalDag(){
            for(Personeelslid lid : leden){
                System.out.println("Naam: " + lid.naam + " ; Salaris: " + lid.getLoon());
            }
        }
    
    }

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

    Default Re: A small problem

    Quote Originally Posted by Imstuck View Post
    JoSah yes that's most probably the problem, how do I change it though? don't i suggest it's a worker when i say member[0] = new Worker("Jan", 1000); ?
    It may be an Arbeider object, but it's held by a Personeelslid array and thus is treated as a Personeelslid variable.

    Here is the full code (it's in dutch but you'll hopefully understand)
    I've got a sneaking suspicion that JosAH will understand this (for me) confusing and confounding language.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default Re: A small problem

    Quote Originally Posted by Fubarable View Post
    I've got a sneaking suspicion that JosAH will understand this (for me) confusing and confounding language.
    What's wrong with a language that sounds like clearing your throat all the time? The class hierarchy is broken. Every staff member is supposed to work so the work( ... ) method and its compadres don't belong in the Worker (Arbeider) class; they belong in the StaffMember (Personeelslid) parent class. A hackish solution o.t.o.h. would be a downcast, but I consider that, just hacking ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: A small problem

    Quote Originally Posted by JosAH View Post
    What's wrong with a language that sounds like clearing your throat all the time?
    I have problems with *all* of the Germanic languages. ... including my own!

  10. #10
    Imstuck is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: A small problem

    Quote Originally Posted by JosAH View Post
    What's wrong with a language that sounds like clearing your throat all the time? The class hierarchy is broken. Every staff member is supposed to work so the work( ... ) method and its compadres don't belong in the Worker (Arbeider) class; they belong in the StaffMember (Personeelslid) parent class. A hackish solution o.t.o.h. would be a downcast, but I consider that, just hacking ...

    kind regards,

    Jos
    Problem is there are other subclasses with Staffmember as superclass, so work is specific for worker. :)
    Last edited by Imstuck; 11-18-2012 at 06:36 PM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default Re: A small problem

    Quote Originally Posted by Imstuck View Post
    Problem is their are other subclasses with Staffmember as superclass, so work is specific for worker. :)
    Why not have an array of Worker objects then? That way you can be sure that all elements can work( ... )

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default Re: A small problem

    Quote Originally Posted by Fubarable View Post
    I have problems with *all* of the Germanic languages. ... including my own!
    I can order two large beer from the tap in Swahili ...

    kind regards,

    Jos (<--- true polyglot ;-)
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Imstuck is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: A small problem

    Quote Originally Posted by JosAH View Post
    Why not have an array of Worker objects then? That way you can be sure that all elements can work( ... )

    kind regards,

    Jos
    It's an assignment and the array has to be of the superclass and i have to use the subclass method. I hope someone can help, I've been thinking about this for hours and deadline is in 20 mins. :/

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,682
    Blog Entries
    7
    Rep Power
    21

    Default Re: A small problem

    Then use the instanceof operator together with a downcast (a hack):

    Java Code:
    if (elements[i] instanceof Worker)
       ((Worker)elements[i]).work( ... );
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Small JAR problem
    By Kaizer in forum New To Java
    Replies: 11
    Last Post: 12-16-2011, 08:16 PM
  2. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 04:43 AM
  3. small problem
    By rawan in forum Java Applets
    Replies: 2
    Last Post: 04-07-2010, 04:32 AM
  4. small problem
    By barusk in forum Networking
    Replies: 4
    Last Post: 03-21-2009, 07:19 AM
  5. Small problem
    By ayoood in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 01:27 PM

Posting Permissions

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