I just started learning primitive Data Types and Operations and I am writing a program to receive 4 float values input from a popup window (showInputDialog method). I have to find things like average, product, sum, and (here�s the biggy) largest/smallest (and display in Output = showMessageDialog method).
I can do everything except largest/smallest (I compiled and ran, and everything else works). I could use a hint on how to find the largest/smallest value (e.g. among 4 floats) by just using Primitive Data Types and Operations. I keep on remembering (from college 20 years ago) about something like if/then/else statements, but I am not there yet. So I am stuck with +, -, *, /, &&, ||, <, >, etc.
Any help is appreciated.
Mitch
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
Take a look at the javadoc for Math. You should spot something useful.
If you really want to use only primitives and operators - not the Math class - look at if and > as in:
if (a > b) do something
If you're really rusty on things like "if" you'll need a good language reference. Somebody with more brain cells than I will chime in with links to the Sun tutorial and a list of online books and such.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
look up the api docs for Math.min() and Math.max()
declare variables float min,max;
both min and max will be your first input.
each subsequent input you recheck min and max min = Math.min(min,newInput); max = Math.max(max,newInput);
Mitch Krah
Ranch Hand
Joined: Sep 06, 2004
Posts: 41
posted
0
Thank you!
Originally posted by Stan James: Take a look at the javadoc for Math. You should spot something useful.
If you really want to use only primitives and operators - not the Math class - look at if and > as in:
if (a > b) do something
If you're really rusty on things like "if" you'll need a good language reference. Somebody with more brain cells than I will chime in with links to the Sun tutorial and a list of online books and such.
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
Welcome to JavaRanch, Mitch!
Originally posted by Stan James: Somebody with more brain cells than I will chime in with links to the Sun tutorial and a list of online books and such.
Until that somebody makes an appearance, I thought you mike like to have the following list of free on-line Java tutorials and books that I have found useful:
Originally posted by Dirk Schreckmann: Until that somebody makes an appearance, I thought you mike like to have the following list of free on-line Java tutorials and books that I have found useful:
Dirk, perhaps you shoud put that somewhere in the FAQ?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus