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.

Signature
John T.
My other computer is a swodniw machine
2007
Roedy Green - 28 Jul 2007 09:08 GMT
>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
ArrayLists are for when you don't know in advance how many elements
you will have. Arrays are for when you do. If you flip your code
from ArrayList to Array you will see it becomes much more readable and
faster.
Not to worry about the loss of Collections.sort. See Arrays.sort.
See http://mindprod.com/jgloss/sort.html
also try to convert any FOR to for:each.. That greatly enhances
readability.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
JT - 28 Jul 2007 10:14 GMT
>>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.
>>
[quoted text clipped - 10 lines]
> also try to convert any FOR to for:each.. That greatly enhances
> readability.
Yeah, I knew I could have implemented a bubble sort or insertion sort or
something like that, but the Collections.sort lets me do it in one line.
And thanks for the tip about for:each I knew there was something like
that but I didn't feel like looking it up :-)
Roedy Green - 28 Jul 2007 21:33 GMT
>Yeah, I knew I could have implemented a bubble sort or insertion sort or
>something like that, but the Collections.sort lets me do it in one line.
so does Arrays.sort which sorts arrays. See
http://mindprod.com/jgloss/sort.html
There is no need to roll your own sort to sort arrays.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Patricia Shanahan - 28 Jul 2007 11:46 GMT
> I gave it a shot, to answer the question that that person posted here a couple of days ago
>
[quoted text clipped - 17 lines]
>
> 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
JT - 28 Jul 2007 12:09 GMT
>> I gave it a shot, to answer the question that that person posted here a couple of days ago
>>
[quoted text clipped - 35 lines]
>
> 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.
John - 28 Jul 2007 22:39 GMT
New version of programs
StatPackTester.java (invokes StatisticsPackage and displays results)
http://sparklesthecat.is-a-geek.com/code/StatPackTester.java
StatisticsPackage.java (does all the work)
http://sparklesthecat.is-a-geek.com/code/StatisticsPackage.java
I don't think there are any magic values, although some of the class variables have obscure names.
Cheers

Signature
John T.
My other computer is a swodniw machine
2007
Oliver Wong - 30 Jul 2007 18:50 GMT
> New version of programs
>
[quoted text clipped - 7 lines]
> I don't think there are any magic values, although some of the
> class variables have obscure names.
If a variable name is unclear, consider renaming the variable
something clearer, or at worst, add a comment explaining what the variable
is for.
Also note that the term "Package" has a very specific meaning in Java,
and I was thrown off when I saw your class was called "StatisticsPackage".
- Oliver
Patricia Shanahan - 28 Jul 2007 17:07 GMT
> I gave it a shot, to answer the question that that person posted here a couple of days ago
>
[quoted text clipped - 6 lines]
> - the median number (50% is above it, 50% is below it)
> - the mode (the number(s) that occurs most frequently)
Snippet from the code:
for (int y=0;y<totalNumberOfDraws;y++) {
stdDev = stdDev + (((double)listOfRandomNumbers.get(y) - median) *
((double)listOfRandomNumbers.get(y) - median));
}
stdDev = stdDev/5;
stdDev = Math.sqrt(stdDev);
1. Why based on median, rather than mean?
2. Why divide by magic constant "5"? (By "magic constant" I mean a
number that just appears in the code, with no explanation.).
Patricia
John - 28 Jul 2007 20:07 GMT
> 1. Why based on median, rather than mean?
I looked up a formula on the internet and it told me to use the mean, and I mis-read it as media. Is mean the same as average? I haven't done stats in a long time.
> 2. Why divide by magic constant "5"? (By "magic constant" I mean a
> number that just appears in the code, with no explanation.).
Typo.. sorry... s/b totalNumberOfDraws

Signature
John T.
My other computer is a swodniw machine
2007
Patricia Shanahan - 28 Jul 2007 20:20 GMT
>> 1. Why based on median, rather than mean?
>
> I looked up a formula on the internet and it told me to use the mean,
> and I mis-read it as media. Is mean the same as average? I haven't
> done stats in a long time.
The term "average" is usually used to for the "mean". "Mean" is more
precise. Your "average" calculation is correct for getting the mean.
Patricia
Oliver Wong - 30 Jul 2007 18:48 GMT
>I gave it a shot, to answer the question that that person posted
>here a couple of days ago
[quoted text clipped - 7 lines]
> - the median number (50% is above it, 50% is below it)
> - the mode (the number(s) that occurs most frequently)
[...]
> So I wrote my program, tested it with some small numbers.
[...]
> But is it wel written? And I don't mean comments. I mean does
> it follow the OO rules?
For some problems, OO is the best tool for the job. For this problem,
I don't think OO is the best tool for the job. The problem specification
reads like a recipe, listing each step to be performed, and the order in
which to perform those steps. It is extremely natural to translate the
problem description almost directly into a procedural (i.e. non-OO)
program.
- Oliver
JT - 30 Jul 2007 21:47 GMT
> For some problems, OO is the best tool for the job. For this problem,
> I don't think OO is the best tool for the job. The problem specification
[quoted text clipped - 4 lines]
>
> - Oliver
So what is the point of using Java if you aren't going to employ OO
techniques? I could have written it in Cobol, or Fortran or even...
gasp... C (well maybe not in C as fast as I could in Cobol) the assignment
was in a Java class :-) And I didn't interpret the assignment as do this,
then this, then this.... all I saw was a bunch of results that the
instructor wanted out the other end. And the only pre-requisite that I
could see was that the mean had to be computed before the stdDev as it was
used. Although, it would have been just as easy to include a computation
of the mean in the section that does stdDev, or better yet, call the
setStdDev method within.. but that seemed like overkill to me. So I simply
pass the mean into the stdDev method.
Anyways, it was an interesting exercise. My first program was a piece of
crap and it had bugs in it (thanks for spotting them Patricia). I think
my new program is pretty good, although there are probably improvements to
be made. My next iteration will probably make some of them.
Bjorn Abelli - 30 Jul 2007 22:08 GMT
"JT" <jtlinux1@oohay.ca> wrote...
>> For some problems, OO is the best tool for the job. For this problem,
>> I don't think OO is the best tool for the job. The problem specification
[quoted text clipped - 7 lines]
> So what is the point of using Java if you aren't going
> to employ OO techniques?
Simply because it can be done? ;-)
Many programming classes, "focusing" on different languages, are overlapping
in terms of fundamental algorithmic thinking.
This type of excercise is useful regardless of which programming language
that is taught.
What many classes (teachers) usually miss, is to point out the differences
between different solutions; how different languages and programming
paradigms connect to each other, and the consequences of that.
/// Bjorn A