aspose file tools*
The moose likes Beginning Java and the fly likes Displaying the largest and smallest of three integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Displaying the largest and smallest of three integers" Watch "Displaying the largest and smallest of three integers" New topic
Author

Displaying the largest and smallest of three integers

Chris Rothgeb
Greenhorn

Joined: Mar 15, 2008
Posts: 11
I wrote a simple program that requires a user to input three integers. The output is to show the sum, difference, product, quotient, as well as the largest and smallest of the three integers. Everything works accept for the largest and smallest integers being displayed. I cannot find a solution to make the program display that part. Can anyone assist me with that part of the program. Thanks...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Chris Rothgeb:
... Can anyone assist me with that part of the program. Thanks...

What have you tried so far? And where are you stuck?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Here's a hint:
The maximum of three numbers is the maximum of the maximum of two of the numbers and the remaining number.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Chris Rothgeb
Greenhorn

Joined: Mar 15, 2008
Posts: 11

[ April 07, 2008: Message edited by: fred rosenberger ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

First, please use the 'code' tags when posting source code. this will preserve the spacing, making it easier to read (i edited your post to put them in). you can use the 'Instant UBB Code' buttons to get them in the dialog box, the put your code between them

Second, this code doesn't compile. Please cut-n-paste your original code, rather than re-create it.

Then, assuming the only difference between your code and what you pasted here is a missing declaration of 'result'...

i'm not sure what you are trying to do with the 'difference of three integers'. that doesn't seem to make much sense, mathematically. but in any case, you calculate SOMETHING there, and store it in your 'result' variable.

now look CAREFULLY at what you are doing with your last three if statements, and what you are printing...

as a side note, you probably don't need three if statements, but could simply use a if-else. it is impossible for any more than one of these conditions to be true.

You may want to put some thought into what you'd do if the specs said "let the user input 100 numbers... how would you find the largest or smallest of those?
[ April 07, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
This does not do all of your work for you, but it narrows what you need to be thinking about to get your design challenge implemented in you code. Note several things: It will by design, not be a drop in for your code. Nor will it display anything. It is to serve as a basis for thinking. Also the initialization of biggest int and smallest in is exactly backwards from what your first thought will be. For another thing, it does not try to do the program logic directly on a print statement. The choice of having an array of ints is to avoid sticking on some 'perfectionist' matters and instead focus on a design that is applicable in general.


You can code your problem as you have attempted, just doing enough if()'s to cover all the possibilities.
[ April 07, 2008: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20


Please note that this will round down (or up if the average is negative) the average to an integer number. If you want the decimal part as well you should use a float or double; divide by 3.0 for double and 3.0f for float.
neilson ramalho
Greenhorn

Joined: Apr 07, 2008
Posts: 10
Ok, considering that you're probably starting with Java, let's keep things simple. First of all, try to forget about C programming... use System.out.println("String" + value) instead of
System.out.printf("The average is %d\n", result);

in your code, you're not printing the smallest number here
System.out.printf("The product of three integers is %d\n", result);
you're actually printing the last value of "result", which is
result = (x - y - z);

here is your code with some corrections...


have fun,

Neilson

[ April 07, 2008: Message edited by: neilson ramalho ]
[ April 07, 2008: Message edited by: fred rosenberger ]
Chris Rothgeb
Greenhorn

Joined: Mar 15, 2008
Posts: 11
Thank you for all the information. I will take a closer look at the sections pointed out in your responses. Also, my apologies for the improper posting. I will make sure I do it correctly in the future.
Thanks again...Chris Rothgeb
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Originally posted by Nicholas Jordan:

Why use an Integer and not an int?
[ April 07, 2008: Message edited by: Rob Prime ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Rob Prime:
Why use an Integer and not an int?


No paticular reason, poster has some work to do and I developed that style to avoid autoboxing doing un-anticipated something or other that I read once and did not have the time to absorb:   .intValue()   on an Integer is just there to avoid having the limit value subject to unanticipated changing later. Integers are immutable, so is final - I went to effort to notifiy poster the code was for working on concepts in his mind, not a totally thought out solution.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Chris Rothgeb:
(...snip...) Also, my apologies for the improper posting. I will make sure I do it correctly in the future.(...snip...)


Pass any heat off to me, focus on re-developing you code dozens of times. Take occasional diversionary breaks about five to thrity five percent of the time. You will find as you revisit code that skills get stronger with practice. The practice resembles any repetitive task and must be balanced.

There are moments of great frustration, the masters have just mastered the basics and nothing else. (well, sort of.....)

  <- very normal.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Nicholas Jordan:
...to avoid autoboxing doing un-anticipated something or other that I read once and did not have the time to absorb...

I'm guessing this is if the value is in the range of a byte, then two separate autoboxing conversions on that value will reference the same wrapped instance. If so, that shouldn't be a concern here, so I would use stick with int values.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by marc weber:
I'm guessing this is if the value is in the range of a byte, then two separate autoboxing conversions on that value will reference the same wrapped instance. If so, that shouldn't be a concern here, so I would use stick with int values.


That's fine, as the poster may understand it. What I was tryingt to do is to disentangle caclulations, logic, data model and presentation. Perhaps the poster will do better in OO, Patterns, UML and Refactoring. It was not the finality of a limit value nor using an Object to wrap a primitive that I was trying to attain. I have spent wasted hours trying to do it like the poster had it, with things like

if(y <= x && y <= z )

I write this way all the time now, but I bust it out from the display of the data. I considered your point while writing the sample but was well into the next several lines before I could even arrive at a decision. The basis of my decision was that if the int was variable, then autoboxing is a compiler issue. As I have it the poster could as well declare a final int, wouldn't really matter, but it would matter if poster tried to do it as ((()())()), that causes beginners to do some inefficient thinking.

Limit values should be final, no?
[ April 08, 2008: Message edited by: Nicholas Jordan ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The only thing being done with "Limit" here is to convert it back to an int. Surely it's simpler to just use an int in the first place:

Introducing wrapper classes here yields absolutely no benefits - it just makes things a little harder to read, and slows down the JVM with a few pointless method calls.

Of course the original goal was just to iterate through an array. This is even easier with a for loop:

There's no need to introduce complications about autoboxing here. The original problem was simple, so let's keep the discussion simple.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying the largest and smallest of three integers