Why won't this compile?

Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Why won't this compile?

 
0
  #1
Dec 3rd, 2007
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

  1. public class Worker
  2. {
  3. public Worker(String n, int r)
  4. {
  5. name = n;
  6. rate = r;
  7. }
  8. public double computePay(int hours)
  9. {
  10. return (rate * hours);
  11. }
  12. public int getRate()
  13. {
  14. return rate;
  15. }
  16. private String name;
  17. private int rate;
  18. }

SalariedWorker
  1. public class SalariedWorker extends Worker
  2. {
  3. public SalariedWorker(String n, int r)
  4. {
  5. super(n, r);
  6. }
  7. public double computePay(int hours)
  8. {
  9. double totalPay = 0;
  10.  
  11. totalPay = this.getRate() * 40;
  12.  
  13. return totalPay;
  14. }
  15. }

HourlyWorker.java
  1. public class HourlyWorker extends Worker
  2. {
  3. public HourlyWorker(String n, int r)
  4. {
  5. super(n, r);
  6. }
  7. public double computePay(int hours)
  8. {
  9. double totalPay = 0;
  10.  
  11. if (hours >= 0 && hours <= 40)
  12. totalPay = this.getRate() * hours;
  13. else
  14. totalPay = this.getRate() * ((hours - 40) * 1.5 + 40);
  15.  
  16. return totalPay;
  17. }
  18. }

/**
This class tests class Worker and its subclasses.
*/
WorkerTester.java
  1. public class WorkerTester
  2. {
  3. public static void main(String[] args)
  4. {
  5. Worker s = new SalariedWorker("Sally", 40);
  6. Worker h = new HourlyWorker("Harry", 40);
  7.  
  8. System.out.println(s.computePay(30));
  9. System.out.println("Expected: 1600");
  10. System.out.println(h.computePay(30));
  11. System.out.println("Expected: 1200");
  12. System.out.println(s.computePay(50));
  13. System.out.println("Expected: 1600");
  14. System.out.println(h.computePay(50));
  15. System.out.println("Expected: 2200");
  16. }
  17. }
Last edited by degamer106; Dec 3rd, 2007 at 4:07 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,388
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Why won't this compile?

 
0
  #2
Dec 3rd, 2007
compile with
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Why won't this compile?

 
0
  #3
Dec 3rd, 2007
Still getting the same problem.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 62
Reputation: Black Box is on a distinguished road 
Solved Threads: 7
Black Box Black Box is offline Offline
Junior Poster in Training

Re: Why won't this compile?

 
0
  #4
Dec 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,388
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Why won't this compile?

 
0
  #5
Dec 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Why won't this compile?

 
0
  #6
Dec 3rd, 2007
Show your exact directory structure, and the exact command you used to compile.
c:\fall2007\test\Homework\Due Dec 5> javac WorkerTester.java

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
Everything is in the same directory.

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: satya5321 is an unknown quantity at this point 
Solved Threads: 0
satya5321 satya5321 is offline Offline
Newbie Poster

Re: Why won't this compile?

 
0
  #7
Dec 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,388
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Why won't this compile?

 
0
  #8
Dec 17th, 2007
Originally Posted by satya5321 View Post
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.
This has nothing to do with a default constructor. And, you never have to implement a default constructor. Only if you want to.

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,388
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Why won't this compile?

 
0
  #9
Dec 17th, 2007
Originally Posted by degamer106 View Post
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.
"." 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC