| | |
Why won't this compile?
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
This is a relatively simple program but for some reason, I can't compile it. I'm pretty sure the code is correct and working.
My compiler keeps saying that it cannot find a constructor matching a HourlyWorker constructor when I instantiate a HourlyWorker object. I've checked and rechecked the code (in WorkerTester and HourlyWorker classes) to make sure everything matches, but I can't find anything wrong.
Worker.java
SalariedWorker
HourlyWorker.java
/**
This class tests class Worker and its subclasses.
*/
WorkerTester.java
My compiler keeps saying that it cannot find a constructor matching a HourlyWorker constructor when I instantiate a HourlyWorker object. I've checked and rechecked the code (in WorkerTester and HourlyWorker classes) to make sure everything matches, but I can't find anything wrong.
Worker.java
Java Syntax (Toggle Plain Text)
public class Worker { public Worker(String n, int r) { name = n; rate = r; } public double computePay(int hours) { return (rate * hours); } public int getRate() { return rate; } private String name; private int rate; }
SalariedWorker
Java Syntax (Toggle Plain Text)
public class SalariedWorker extends Worker { public SalariedWorker(String n, int r) { super(n, r); } public double computePay(int hours) { double totalPay = 0; totalPay = this.getRate() * 40; return totalPay; } }
HourlyWorker.java
Java Syntax (Toggle Plain Text)
public class HourlyWorker extends Worker { public HourlyWorker(String n, int r) { super(n, r); } public double computePay(int hours) { double totalPay = 0; if (hours >= 0 && hours <= 40) totalPay = this.getRate() * hours; else totalPay = this.getRate() * ((hours - 40) * 1.5 + 40); return totalPay; } }
/**
This class tests class Worker and its subclasses.
*/
WorkerTester.java
Java Syntax (Toggle Plain Text)
public class WorkerTester { public static void main(String[] args) { Worker s = new SalariedWorker("Sally", 40); Worker h = new HourlyWorker("Harry", 40); System.out.println(s.computePay(30)); System.out.println("Expected: 1600"); System.out.println(h.computePay(30)); System.out.println("Expected: 1200"); System.out.println(s.computePay(50)); System.out.println("Expected: 1600"); System.out.println(h.computePay(50)); System.out.println("Expected: 2200"); } }
Last edited by degamer106; Dec 3rd, 2007 at 4:07 am.
compile with
Java Syntax (Toggle Plain Text)
javac -classpath . <Classname>.java
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Nov 2007
Posts: 62
Reputation:
Solved Threads: 7
I've copied your code and it works perfectly. How do you have your files organized?
Like this?
-WorkerDirectory
+ Worker.java
+ HourlyWorker.java
+ SalariedWorker.java
+ WorkerTester.java
Some package definitions lacking maybe?
Black Box
Like this?
-WorkerDirectory
+ Worker.java
+ HourlyWorker.java
+ SalariedWorker.java
+ WorkerTester.java
Some package definitions lacking maybe?
Black Box
Show your exact directory structure, and the exact command you used to compile.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
•
•
•
•
Show your exact directory structure, and the exact command you used to compile.
The WorkerTester.java file as well as all the other files associated with it is located in the directory, so I don't think I have to specify the classpath as a parameter.
I get the same compile-time error as when I compile the file with Textpad; can't find an HourlyWorker constructor matching the declaration.
•
•
•
•
I've copied your code and it works perfectly. How do you have your files organized?
Like this?
-WorkerDirectory
+ Worker.java
+ HourlyWorker.java
+ SalariedWorker.java
+ WorkerTester.java
Some package definitions lacking maybe?
Black Box
edit: hmm..weird. I tried to compile the files on the compiler at school and it works fine.
Last edited by degamer106; Dec 3rd, 2007 at 6:48 pm.
•
•
Join Date: Dec 2007
Posts: 10
Reputation:
Solved Threads: 0
hi from my understanding there is no default constructor defined in any of the Classes that you wrote.when you are overloading constructor the default constructor is not provided. externally you have to write the default constructor
tell me i am wrong.
tell me i am wrong.
•
•
•
•
hi from my understanding there is no default constructor defined in any of the Classes that you wrote.when you are overloading constructor the default constructor is not provided. externally you have to write the default constructor
tell me i am wrong.
Edit: Well, I shouldn't really say never, but at least the cases where you might have to are not that common, and definately not a problem in this case.
Last edited by masijade; Dec 17th, 2007 at 4:15 am.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
"." is not necessarily on the classpath, and if it is not, then yes, you do have to add it. So did you actually try with the command I showed you? Or did you just assume that it wouldn't make a difference? Also, try to compile one of the classes that does not depend on any of the others, and then make sure that the classfile actually appears in the same directory as the java file (not that you have some alias set that automatically causes the compiler to place the class files elsewhere). If it is there, then compile them one at a time from the file with the least dependencies, to the file with the most. More than bit annoying, but hey.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- visual c++ strange compile error... (C++)
- Compile PHP (PHP)
- static compile (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Something I don't Quite Understand About the Container Class...
- Next Thread: midlet
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing text-file thread threads tree windows






