Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Counting in Array List/ Standard deviation

 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm working on an assignment that I can't seem to get my arms around. Basically, I have to supply values and count the number of entries, provide a sum for each number added,an average and standard deviation. Right now, I'm trying to correctly add each entry into the list and get the count, sum and average. For some reason, it doubles each entry within the LIst, so that if I provide one entry of '5', it adds it twice when getting the sum.

Please review what I have and provide any/all the help you can. Thank you.

This is the tester class:



Here is the dataset class
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep.

On row 34 you add the new number to the sum.

On row 43 you start to loop and add the new number again.

Why not make a method that sums it up when asked to do it? One method should do one thing. Add-method should add the number to the list, sum-method return the sum. avg-method the average.
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Yes, I'm working towards cleaning it up, but I'm really stuck on the array. I've corrected and here is the new Dataset class and output. Seems to still be doubling in the add() method.




Output:

 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ask yourself: Why do I need to loop through all the numbers already added in the list and for every number found there, add x to the sum?

I've done something like this:

 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point..lol. Thanks for the help tonight. Ron
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To follow up in case someone is left working the same problem, I've completed this program using the following Dataset class:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic