Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a better way to do this?

1 view
Skip to first unread message

John

unread,
Jul 27, 2007, 9:02:55 PM7/27/07
to
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.


--
John T.
My other computer is a swodniw machine
2007

Roedy Green

unread,
Jul 28, 2007, 4:08:57 AM7/28/07
to
On 28 Jul 2007 01:02:55 GMT, John <jtli...@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.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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

JT

unread,
Jul 28, 2007, 5:14:38 AM7/28/07
to

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 :-)

Patricia Shanahan

unread,
Jul 28, 2007, 6:46:25 AM7/28/07
to

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

unread,
Jul 28, 2007, 7:09:40 AM7/28/07
to

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

Patricia Shanahan

unread,
Jul 28, 2007, 12:07:09 PM7/28/07
to
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

John

unread,
Jul 28, 2007, 3:07:14 PM7/28/07
to
On 2007-07-28, Patricia Shanahan <pa...@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 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

Patricia Shanahan

unread,
Jul 28, 2007, 3:20:09 PM7/28/07
to
John wrote:
> On 2007-07-28, Patricia Shanahan <pa...@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 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

Roedy Green

unread,
Jul 28, 2007, 4:33:07 PM7/28/07
to
>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.

John

unread,
Jul 28, 2007, 5:39:09 PM7/28/07
to
On 2007-07-28, JT <jtli...@oohay.ca> wrote:
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

Oliver Wong

unread,
Jul 30, 2007, 1:48:29 PM7/30/07
to

"John" <jtli...@oohay.ca> wrote in message
news:46aa95be$0$4052$9a56...@news.aliant.net...

>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)
>
[...]

> 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


Oliver Wong

unread,
Jul 30, 2007, 1:50:58 PM7/30/07
to

"John" <jtli...@oohay.ca> wrote in message
news:46abb77d$0$4025$9a56...@news.aliant.net...

> On 2007-07-28, JT <jtli...@oohay.ca> wrote:
> 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.

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


JT

unread,
Jul 30, 2007, 4:47:14 PM7/30/07
to
On Mon, 30 Jul 2007 13:48:29 -0400, Oliver Wong 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
> 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

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

unread,
Jul 30, 2007, 5:08:13 PM7/30/07
to

"JT" <jtli...@oohay.ca> wrote...

> On Mon, 30 Jul 2007 13:48:29 -0400, Oliver Wong 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
>> 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
>
> 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


0 new messages