Is there a better way to do this?  Java
This is a discussion on Is there a better way to do this?  Java ; 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:
...

Is there a better way to do this?
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 1500 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 overdone 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.isageek.com/code/Hello.java
I know it's not a very imaginative name, but it's not going on the market.

John T.
My other computer is a swodniw machine
2007

Re: Is there a better way to do this?
On 28 Jul 2007 01:02:55 GMT, John <jtlinux1@oohay.ca> wrote, quoted or
indirectly quoted someone who said :
>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.isageek.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.

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Re: Is there a better way to do this?
On Sat, 28 Jul 2007 08:08:57 +0000, Roedy Green wrote:
> On 28 Jul 2007 01:02:55 GMT, John <jtlinux1@oohay.ca> wrote, quoted or
> indirectly quoted someone who said :
>
>>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.isageek.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.
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 :)

Re: Is there a better way to do this?
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 1500 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 overdone 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.isageek.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 ****yze an arbitrary
List<Integer>.
Patricia

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 1500 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 overdone 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.isageek.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 ****yze an arbitrary
> List<Integer>.
>
> Patricia
Agreed. Time for a refactoring. One class does one thing and one thing
well. This class does several things. Thanks for the hint.

Re: Is there a better way to do this?
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)
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

Re: Is there a better way to do this?
On 20070728, Patricia Shanahan <pats@acm.org> wrote:
>
> 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 misread 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

John T.
My other computer is a swodniw machine
2007

Re: Is there a better way to do this?
John wrote:
> On 20070728, Patricia Shanahan <pats@acm.org> wrote:
>
>> 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 misread 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

Re: Is there a better way to do this?
>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.

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Re: Is there a better way to do this?
On 20070728, JT <jtlinux1@oohay.ca> wrote:
New version of programs
StatPackTester.java (invokes StatisticsPackage and displays results)
http://sparklesthecat.isageek.com/...ackTester.java
StatisticsPackage.java (does all the work)
http://sparklesthecat.isageek.com/...csPackage.java
I don't think there are any magic values, although some of the class variables have obscure names.
Cheers

John T.
My other computer is a swodniw machine
2007