Re: Is there a better way to do this?



On Sat, 28 Jul 2007 10:46:25 +0000, Patricia Shanahan wrote:

John wrote:
I gave it a shot, to answer the question that that person posted here a couple of days ago

Create a java program that generates 10000 random integer numbers
between 1 and 500. Then use the generated numbers to calculate:

- the mean (average) of all numbers
- the standard deviation
- the largest and smallest number
- the median number (50% is above it, 50% is below it)
- the mode (the number(s) that occurs most frequently)

The hardest part was the "mode" part.
At first I thought I'd use arrays, but then I realized that using an ArrayList<Integer> would let me use the various methods associated with the Collections class

So I wrote my program, tested it with some small numbers. It looks ok, and when I run it with 10000 occurances of randoms 1-500 I get decent results. And it does answer the question, so I'd probably get 50% on it for sure. But is it wel written? And I don't mean comments. I mean does it follow the OO rules? I put one static method in to fill up the ArrayList of randoms, but can I take it further? Or is there a point where you've over-done it?

Anyway, here's a link to my code. I'd appreciate any comments or critisim. I think it's a good first attempt, but it's not something I'd want to turn in to a professor, yet.

http://sparklesthecat.is-a-geek.com/code/Hello.java


I know it's not a very imaginative name, but it's not going on the market.

As well as being unimaginative, it allowed you to avoid deciding the
primary purpose of the class. One of the first tests of a "good" class
is that it is easy to name, because it has a known purpose.

In this case, I think Hello combines two jobs:

1. A statistics calculator.

2. A test wrapper that supplies the statistics calculator with a List of
random numbers and prints results.

The test wrapper could be tucked away in the main method of the
statistics calculator, but I think it should be separated out so that
the statistics calculator could be used to analyze an arbitrary
List<Integer>.

Patricia

Agreed. Time for a re-factoring. One class does one thing and one thing
well. This class does several things. Thanks for the hint.
.