Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2007

Tip: Looking for answers? Try searching our database.

Help -- New to Java

Thread view: 
Al - 26 Jul 2007 01:53 GMT
Hello,

I am taking a java class for the first time. The first HW was easy. I
am stuck on the 2nd. I know the logic, I can write it in C, but with
Java I am having problems and it is due tomorrow. Is there any site
where I can find small sample programs. Appreciate any help. This is
my HW prob.

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)

Hints:
-    Use  the java.lang.Math class
-    Write a static method to generate the numbers.
-    For each type of calculation, use a separate static method
(function) returning an integer or double value.
-    Use an array defined in the main() function to store the numbers.
Pass the array as a parameter to the other functions.
-    Use java.util.JOptionPane to display all the results in one single
dialog.
-    The class name should be StatFunctionsXXX where XXX is your name.

Thank you,
Al
Jeff - 26 Jul 2007 02:52 GMT
> Hello,
>
[quoted text clipped - 26 lines]
> Thank you,
> Al

What have you written so far? Other than giving us the assignment,
what more specific questions do you have? Where are you stuck?
Bjorn Abelli - 26 Jul 2007 07:19 GMT
"Al" <doubletrouble_s@yahoo.com> wrote...

> I am taking a java class for the first time. The first HW was easy. I
> am stuck on the 2nd. I know the logic, I can write it in C, but with
> Java I am having problems and it is due tomorrow.

If you can write it in C, I don't really see the problem.

It should be pretty much the same in Java.

The small differences I can imagine from your "old practice" of C could be
the declaration/definition parts for arrays and methods.

 int[] numbers = new int[10000];

...instantiates an array with 10000 numbers (all zero), which you can loop
through to give random values.

 public static int double(int[] p) {

  // Here you put in the logic to calculate the average

 }

The main difference from C in this aspect is that you need to use the
keyword "static". Otherwise it becomes an "instance member" instead of a
"class member".

> Is there any site where I can find small sample programs.
> Appreciate any help. This is my HW prob.

http://www.google.com/search?q=Java+code+examples
http://www.google.com/search?q=Java+code+snippets

...and of course:

http://java.sun.com/docs/books/tutorial/getStarted/

...to begin with...

More specific examples for your assignment you can find through these pages:

> - The class name should be StatFunctionsXXX where XXX is your name.

http://www.google.com/search?q=Java+defining+class+example

> - Use an array defined in the main() function to store the numbers.

http://www.google.com/search?q=Java+array+example

> - Use  the java.lang.Math class

http://java.sun.com/javase/6/docs/api/java/lang/Math.html

> - Write a static method to generate the numbers.
> - For each type of calculation, use a separate static method
> (function) returning an integer or double value.

http://www.google.com/search?q=Java+static+method+example

> - Use java.util.JOptionPane to display all the results in one
> single dialog.

http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html

/// Bjorn A
Lew - 26 Jul 2007 14:54 GMT
>   public static int double(int[] p) {

You'll need a method name that isn't a keyword.

>    // Here you put in the logic to calculate the average
>
[quoted text clipped - 3 lines]
> keyword "static". Otherwise it becomes an "instance member" instead of a
> "class member".

No, you don't.  This could just as well be an instance method.

Signature

Lew

Bjorn Abelli - 26 Jul 2007 16:48 GMT
>>   public static int double(int[] p) {
>
> You'll need a method name that isn't a keyword.

I changed it just before I sent it, from

   public static int mean(int[] p) {

...as I intended to change the return type, but changed at the wrong
place... ;-)

>> The main difference from C in this aspect is that you need to use the
>> keyword "static". Otherwise it becomes an "instance member" instead of a
>> "class member".
>
> No, you don't.  This could just as well be an instance method.

Yes, he does, as his assignment explicitly says so... ;-)

/// Bjorn A
Lew - 26 Jul 2007 16:57 GMT
>>> The main difference from C in this aspect is that you need to use the
>>> keyword "static". Otherwise it becomes an "instance member" instead of a
>>> "class member".
>> No, you don't.  This could just as well be an instance method.
>
> Yes, he does, as his assignment explicitly says so... ;-)

You are correct.  I was speaking outside the context of the OP's homework,
wasn't I?

Actually, for methods the question of static vs. instance interests me.  Is
there a best practice (when a manager hasn't forced the choice for
non-technical reasons)?

I lean towards making methods instance-level most of the time.  For
non-instantiable utility classes I use only static methods and no instance
anythings.

Most of my instance methods are final (as their parameters usually are).

I figure this will give me the most power when I need to expand, secure or
make thread-safe my classes, and doesn't carry a discernible penalty.

Some methods are inherently static, typically the sort that initialize an
environment.  If the design calls for it, that justifies it.

Signature

Lew

Bjorn Abelli - 26 Jul 2007 17:53 GMT
"Lew" <lew@lewscanon.nospam> wrote...

>>>> The main difference from C in this aspect is that you need to
>>>> use the keyword "static". Otherwise it becomes an "instance
>>>> member" instead of a "class member".

>>> No, you don't.  This could just as well be an instance method.
>>
>> Yes, he does, as his assignment explicitly says so... ;-)
>
> You are correct.  I was speaking outside the context of the OP's
> homework, wasn't I?

I don't know, did you? ;-)

> Actually, for methods the question of static vs. instance interests me.
> Is there a best practice (when a manager hasn't forced the choice for
[quoted text clipped - 3 lines]
> non-instantiable utility classes I use only static methods and no
> instance anythings.

This debate, or at least similar, has been going on for decades, somewhat
similar to discussions on when to make "functions" or "procedures" back in
the days before OO...

Among OO-purists, it's mostly considered bad practice to have static methods
at all.

On the other end of the spectrum, developers stuck in the old procedural way
of programming, some never use instances...

I would say that "best practice" should be quite pragmatic on this issue. If
you do a good OOAD, it usually falls quite naturally "what works". The
problem is rather that there are so many bad OOAD out there...

In this particular case, I guess the OP's forced to use static methods out
of two reasons;

- for some it's easier to grasp Java in the beginning by not using instances

- it follows Java's own use of static methods in the Math class.

/// Bjorn A
John - 27 Jul 2007 02:25 GMT
>>   public static int double(int[] p) {
>
[quoted text clipped - 9 lines]
>
> No, you don't.  This could just as well be an instance method.

I'm going to give his assignment a shot.  All I need is a formula for std dev and median and then that other thing.. I guess google is gonna be my friend.

Signature

John T.
My other computer is a swodniw machine
2007

markacy - 26 Jul 2007 10:40 GMT
> Hello,
>
[quoted text clipped - 26 lines]
> Thank you,
> Al

Hi Al,

  No offence man, but I think You really should attend this classes,
and not only take the assignments. I'm sure they said everything
what's needed to solve Your problem. Besides java class teacher is the
one to ask - he gets paid for this.
  If I'm wrong (for example if You have to work to be able to feed
Your children, and thus can't attend class, or It's a free online
class or something like this), well than - http://www.mindview.net/Books/TIJ/
it's a little bit outdated, but You can read on differences between
java 1.4 and java 1.5 somewhere on the internet. For the very
begginers there are like 7 chapters of this book (4th edition)
available for free as well. I suggest ridding it. Worked well for me.
  One final thing - do as Jeff said.

Cheers,
Marek
Andrew Thompson - 26 Jul 2007 11:13 GMT
You gave such good advice I could not help correcting
the one thing in the post that was technically a mistake..

>...available for free as well. I suggest ridding it. Worked well for me.

That word is 'reading'.  The word 'ridding' (based on 'rid'
to dispose/destroy/throw away or othewise get 'rid' of
something), is a separate word to the one you meant
(I expect).

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Roedy Green - 26 Jul 2007 16:05 GMT
>Create a java program that generates 10000 random integer numbers
>between 1 and 500. Then use the generated numbers to calculate:

see http://mindprod.com/jgloss/homework.html
http://mindprod.com/jgloss/pseudorandom.html
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.