This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Program For Finding Largest Float  RSS feed

 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK! So I am a beginner in a beginner class.

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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the javadoc for Math. You should spot something useful.

Bookmark the JavaDoc. We all use it every day! http://java.sun.com/j2se/1.4.2/docs/api/

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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!

Originally posted by Stan James:
Take a look at the javadoc for Math. You should spot something useful.

Bookmark the JavaDoc. We all use it every day! http://java.sun.com/j2se/1.4.2/docs/api/

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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials
  • Interactive Programming In Java by Lynn Andrea Stein
  • Bruce Eckel's Thinking In Java
  • JavaRanch's own Campfire Stories
  • Allen B. Downey's How To Think Like A Computer Scientist

  • [ September 06, 2004: Message edited by: Dirk Schreckmann ]
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good idea.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!