Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 13th, 2013, 11:42 PM
 stillInBeta
Registered User

Join Date: Aug 2013
Posts: 4
Time spent in forums: 2 h 4 m 31 sec
Reputation Power: 0
Comparing Integers

Hello everyone! I'm new to the forum and pleased to be here.

I'm going through Big Java and scanning the little programming exercises etc. and came across a problem that raised a question. I'd love to hear your thoughts.

The problem is to "write a class DataSet that computes the largest and smallest values in a sequence of numbers" It goes on to give a hint as to utilize Integer.MAX_VALUE and Integer.MIN_VALUE.

Pretty straight forward but I was wondering if there was a way to achieve this without using the above hint and obviously, without utilizing decision statements etc.

This is not homework. I'm just going through past chapters and seeing if I can tackle problems in different ways given the tools learned up to that point of instruction.

#2
August 14th, 2013, 03:43 AM
 Xerixe
Registered User

Join Date: Aug 2013
Posts: 15
Time spent in forums: 1 h 42 m
Reputation Power: 0
Yea, it is possible. Firstly, create an variable for smallest and biggest. Assign the value of the first number to both of them. Then in your loop, compare the numbers, if it's bigger / smaller, replace the appropriate one.

#3
August 14th, 2013, 01:46 PM
 stillInBeta
Registered User

Join Date: Aug 2013
Posts: 4
Time spent in forums: 2 h 4 m 31 sec
Reputation Power: 0
Thanks Xerixe for the response! I guess I should have been more clear in that I'm trying to avoid decision statements or loops.

I know there's really no point but it's more of a puzzle for me as decisions and loops are not discussed until the next chapter. I'm trying to work with what the book as discussed as of that point and without using their hint of Integer.MAX and Integer.MIN values.

Thanks again for the response.

#4
August 14th, 2013, 01:55 PM
 Xerixe
Registered User

Join Date: Aug 2013
Posts: 15
Time spent in forums: 1 h 42 m
Reputation Power: 0
Hm, if you want to use Integer.MAX_VALUE and Integer.MIN_VALUE,
you could do this.
Code:
`int highest = Integer.MIN_VALUE, lowest = Integer.MAX_VALUE;`

Then you could compare from there. Because, since the current value of "highest" is the lowest possible number. Any number would be considered bigger than it.
So from there, you can compare the value of input against "highest" and "lowest".

#5
August 14th, 2013, 02:51 PM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,978
Time spent in forums: 1 Month 2 Weeks 1 Day 9 h 2 m 39 sec
Reputation Power: 1283
Quote:
 Originally Posted by stillInBeta Thanks Xerixe for the response! I guess I should have been more clear in that I'm trying to avoid decision statements or loops. I know there's really no point but it's more of a puzzle for me as decisions and loops are not discussed until the next chapter. I'm trying to work with what the book as discussed as of that point and without using their hint of Integer.MAX and Integer.MIN values. Thanks again for the response.

Out of curiosity, what is the hint?

#6
August 14th, 2013, 02:55 PM
 stillInBeta
Registered User

Join Date: Aug 2013
Posts: 4
Time spent in forums: 2 h 4 m 31 sec
Reputation Power: 0
The books hint is using Integer.MAX_VALUE and Integer.MIN_VALUE.

I'm trying to see if there is a way to do it without using the above hint as well as loops and decisions. Pretty much, swimming with no legs or arms.

#7
August 15th, 2013, 09:34 PM
 JonthnC
Contributing User

Join Date: Sep 2010
Posts: 68
Time spent in forums: 2 Days 2 h 35 m
Reputation Power: 4
Quote:
 ... I was wondering if there was a way to achieve this ... without utilizing decision statements etc.

That's impossible. You have to use loops and if statements for accomplishing that task. At most, you could use recursion to replace the loop, but you would still need an if statement.

You might want to read this to get a better idea.
http://en.wikipedia.org/wiki/Turing_machine

However, Java does come with libraries to do things for you. So if you really don't want a loop or an if statement in your code, you could call Collections.max or Collections.min, but all this would be doing is hiding the implementation behind a method (which is good and possibly more efficient).

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Comparing Integers