• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing a total cost from collection of job Objects in Arraylist

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all as of previous posts here are the class�s which I am using.

The Job Class stores jobs with these parameters

int ref, int nCopy, boolean doubleSided, int nPages, String staffMember, Time atime, Date adate, Date rdate

The Daylog Class stores the jobs in an ArrayList when a job is completed it is passed to the Completed ArrayList

When printing the information to the terminal window I have to display the total cost of all the completed jobs, in the Job class I have the method getCost how can I use this in the Daylog Class to calculate the total cost of the completed jobs and print it to the terminal window.

Any help would be great !
[ January 29, 2006: Message edited by: Iain Linton ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a few suggestions that will help encourage people here to asnwer your questions:

1) Please do not post so much code when you ask a question. Few of us are going to look at every line. You should only post the code that is relavant to your question.

2) You should not start a new thread for a question that is related to previous posts you have made. In other words, you should continue the conversation as a reply to a thread you have already started unless you are asking a completely unrelated question.

3) Ask a specific question. In other words, show us what you have tried so far and explain why your attempts haven't worked out. The more details that you can give, the better.

If you follow these suggestions, you are more likely to get the help you want.

With that said, I have already given you a suggestion in another thread for some steps you can take to figure out how to calculate the total. If you missed them, please go back and reread that thread.

Layne
[ January 28, 2006: Message edited by: Layne Lund ]
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I managed to do it it was a case of =+ thankyou for all your help and advice, now I just have to work out big jobs.

Would it be

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That should work just fine (assuming you have a print() method that will do something relevant). However remember an if statement doesn't always have to have a complemetary else statement. Therefore you could just write this as:

[ January 29, 2006: Message edited by: Garrett Rowe ]
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you now I have to work out the averageCost (mean)using a double.

Any ideas

I thought I would do this:

----------------------------------------------
This does compile but when run get

arithmeticException.

cannot / 0

no help available "Good old BlueJ"

Thank you

[ January 29, 2006: Message edited by: Iain Linton ]
[ January 29, 2006: Message edited by: Iain Linton ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm surprised the compiler let that through since the return type is a void and you're trying to return something. Doing a quick search through your code turns up totalCost being set to 0, but never to anything else. I imagine that is your problem unless you have code that sets totalCost to something. Further, size() will return an integer and you are trying to divide with another integer, so you are going to get integer division (ie, 7/3 = 2) instead of floating point division.

One added note. You don't need to specifically import java.util.Iterator and jave.util.ArrayList since you are also importing java.util.*.


Final Note: I really didn't want to wade through all your code, so if I'm missing something, just skip what I said.
[ January 29, 2006: Message edited by: Craig Tyler ]
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now within the code I have a method which does this:



So when the job is passed to the completedList it also passes its cost to the totalCost field.

then when I have say 4 jobs in the completedList and the totalCost has been updated I would then need to get the totalCost and then divide it by number of jobs in the completedList.

Done this different ways but keeps returning 0.0

I don't understand
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Craig. If youre sure that the totalCost and completedList variables conatain what you think they contain, integer division may be your problem. The workaround for that is using an explicit cast for at least one of your factors.


Note: casting the entire result to a double after the division is performed won't do it i.e.
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition with what Garrett said about explicitly casting one of the operands to use floating point division, your actual division is the wrong way around. To get an average, you need to divide the totalCost by the size of the completedList.
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much you have made my day (well night)

If I have any more problems as Arnold said "I'll be back"
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Craig Tyler:
In addition with what Garrett said about explicitly casting one of the operands to use floating point division, your actual division is the wrong way around. To get an average, you need to divide the totalCost by the size of the completedList.


Good catch Craig, I completely missed the logic error there.
 
reply
    Bookmark Topic Watch Topic
  • New Topic