Results 1 to 14 of 14
Thread: A small problem
- 11-18-2012, 02:14 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
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?
- 11-18-2012, 02:39 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: A small problem
Does that method take parameters?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-18-2012, 02:47 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
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.
- 11-18-2012, 03:23 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-18-2012, 04:07 PM #5
Re: A small problem
Why do they call it rush hour when nothing moves? - Robin Williams
- 11-18-2012, 04:15 PM #6
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
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:
Staffmembers: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; } }
Staff code: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; } }
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()); } } }
-
Re: A small problem
It may be an Arbeider object, but it's held by a Personeelslid array and thus is treated as a Personeelslid variable.
I've got a sneaking suspicion that JosAH will understand this (for me) confusing and confounding language.Here is the full code (it's in dutch but you'll hopefully understand)
- 11-18-2012, 05:09 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: A small problem
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
- 11-18-2012, 05:27 PM #10
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
- 11-18-2012, 05:36 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 11-18-2012, 05:38 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 11-18-2012, 05:40 PM #13
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
- 11-18-2012, 06:16 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Small JAR problem
By Kaizer in forum New To JavaReplies: 11Last Post: 12-16-2011, 07:16 PM -
Small problem with problem with Java, C++ parse program.
By dragstang86 in forum New To JavaReplies: 4Last Post: 10-30-2011, 03:43 AM -
small problem
By rawan in forum Java AppletsReplies: 2Last Post: 04-07-2010, 03:32 AM -
small problem
By barusk in forum NetworkingReplies: 4Last Post: 03-21-2009, 06:19 AM -
Small problem
By ayoood in forum New To JavaReplies: 2Last Post: 06-06-2008, 12:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks