Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Paul Clapham
• Bear Bibeault
• Jeanne Boyarsky
Sheriffs:
• Ron McLeod
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Jj Roberts
• Stephan van Hulst
• Carey Brown
Bartenders:
• salvin francis
• Scott Selikoff
• fred rosenberger

# Calculating the average weekly pay of each employee in an employee array list

Ranch Hand
Posts: 65
I am having trouble figuring out what the best approach to this problem would be with finding the average weekly pay of each employee and I'm looking for suggestions.

How could this be improved?

Saloon Keeper
Posts: 7393
66
You can only divide built in types like int, double, long using the '/' operator. An Employee is not a built in type. I'm guessing that Employee probably has a weeklyPay field that is a built in type. You'd need to sum those and divide by size.

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:You can only divide built in types like int, double, long using the '/' operator. An Employee is not a built in type. I'm guessing that Employee probably has a weeklyPay field that is a built in type. You'd need to sum those and divide by size.

Yeah, I wasn't too sure how to do that when I have 2 computations in different files, I'm sure this sounds a bit more complicated now but this is what I have for the first one and then the 2nd one is below it.

How exactly would I sum each employee since they are different types in this case?

Carey Brown
Saloon Keeper
Posts: 7393
66
You have two different methods. The first computes weekly pay and the second computes monthly pay. So you probably have an error there. If you don't have an error then I suggest renaming them to computeWeeklyPay() and computeMonthlyPay() so as not to confuse them.

The second part of your question I can't answer without more info. Employee is one class in one file, where are these two files of which you speak?

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:You have two different methods. The first computes weekly pay and the second computes monthly pay. So you probably have an error there. If you don't have an error then I suggest renaming them to computeWeeklyPay() and computeMonthlyPay() so as not to confuse them.

The second part of your question I can't answer without more info. Employee is one class in one file, where are these two files of which you speak?

This is the employee file. Then the employee database file below it. Is that what you meant?

Carey Brown
Saloon Keeper
Posts: 7393
66
I see you've tried filling in some of the methods. It is apparent that you haven't tested them because there are a few mistakes. I might suggest starting with the readFile() method and also implement a print() method for debugging. Get that to work first and then, one by one, implement one of the other methods and test it with the data you're reading before going on to the next method.

Regarding your initial question: do you know how to calculate an average?

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:I see you've tried filling in some of the methods. It is apparent that you haven't tested them because there are a few mistakes. I might suggest starting with the readFile() method and also implement a print() method for debugging. Get that to work first and then, one by one, implement one of the other methods and test it with the data you're reading before going on to the next method.

Regarding your initial question: do you know how to calculate an average?

Where are the mistakes at? Also wouldn't the average be the total divided by the size of the array list?

Andrew Bauer
Ranch Hand
Posts: 65
Oh and here's the readFile method now. Does it look fairly correct?

Marshal
Posts: 25963
70

Andrew Bauer wrote:Oh and here's the readFile method now. Does it look fairly correct?

It's hard to tell. The documentation says that it reads the file, which it does. And then it writes the data from the file out to the console, which the documentation doesn't mention. Is it supposed to do that? Elsewhere in the code you've posted there is an Employee class, so you'd think maybe this method is supposed to to create Employee objects from the data? But maybe that comes later and you're just making sure you can read the data correctly.

Which brings up the question: Does that code actually write the correct output to the console? You're the one with the data so you should be able to try it out.

EDIT:

There should be something near the beginning of the code, right after you open the file, which reads this number from the first line. I don't see that in your code.

Andrew Bauer
Ranch Hand
Posts: 65

Paul Clapham wrote:

Andrew Bauer wrote:Oh and here's the readFile method now. Does it look fairly correct?

It's hard to tell. The documentation says that it reads the file, which it does. And then it writes the data from the file out to the console, which the documentation doesn't mention. Is it supposed to do that? Elsewhere in the code you've posted there is an Employee class, so you'd think maybe this method is supposed to to create Employee objects from the data? But maybe that comes later and you're just making sure you can read the data correctly.

Which brings up the question: Does that code actually write the correct output to the console? You're the one with the data so you should be able to try it out.

EDIT:

There should be something near the beginning of the code, right after you open the file, which reads this number from the first line. I don't see that in your code.

Yeah I fixed it, still not entirely sure what exactly is wrong with the other methods now though but here's the readFile again

Marshal
Posts: 70689
288
You are right to close that Scanner, but don't call close(). This tutorials page shows a better way to close it.

Paul Clapham
Marshal
Posts: 25963
70

Andrew Bauer wrote:Yeah I fixed it, still not entirely sure what exactly is wrong with the other methods now though but here's the readFile again

There's something that educators never explicitly tell you, you're supposed to already know it or to observe it. Namely: A computer program is supposed to do something useful, and the main question is "Does this program do what it's supposed to do?"

Now this can get difficult sometimes, because maybe your specifications don't quite explain what it's supposed to do. Or maybe it's hard to tell whether it does what it's supposed to do. But it any case looking at the code is not the way to do that.

So. Does it do what it's supposed to do, or not? And if not, what does it do differently from what it's supposed to do?

Carey Brown
Saloon Keeper
Posts: 7393
66

Andrew Bauer wrote:Where are the mistakes at? Also wouldn't the average be the total divided by the size of the array list?

Yes, but where are you calculating the total? And which Employee are you calculating the pay for?

Andrew Bauer
Ranch Hand
Posts: 65
The pay is being calculated for all of the employees and the method is fixed now as far as I'm concerned.

Campbell Ritchie
Marshal
Posts: 70689
288
Use a for‑each loop (=enhanced for) instead of get(i); get(i) can suffer slow execution depending on thee sort of List you have.
Why are you using instanceof in lines 41, 58, and 129? I always start to suspect there is something wrong with the use of inheritance if I see instanceof. Why is there no method to increase hourly wage rates?

Marshal
Posts: 7791
536

Andrew Bauer wrote: