Lowest of Given numbers.

Hi

I am trying to develop a program which can type the lowest of 3 given integers. I have tried using the "IF-else" statement, but I still think there is a simpler way to do it. I also heard of something called the "Switch Case" Statement, which I am not very sure of. Someone also told me to use the "MIN VALUE" statement. How do I use this. Some help from all of you will be greatly appreciated

Abhay

[421 byte] By [abhaybhargava] at [2007-11-27 9:00:24]
# 1
Show how you are currently doing it please and then we will tell you if theres a better way :-)
ita6cgra at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 2
You could use the java.util.Collections.min-function.-Puce
Pucea at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 3

public class MathHack {

public static int min(int i, int j, int k) {

return i < j && i < k ? i : min(j, k, i);

}

public static void main(String[] args) {

System.out.println("min = "+min(3,1,2));

}

}

prometheuzza at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 4
min(3,3,3);
EvilBroa at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 5
> min(3,3,3);333
aniseeda at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 6
lol uh-oh
ita6cgra at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 7

> min(3,3,3);

You have to go and break it, eh?

; )

public class MathHack {

public static int min(int i, int j, int k) {

return i <= j && i <= k ? i : min(j, k, i);

}

public static void main(String[] args) {

System.out.println("min = "+min(3,1,2));

}

}

prometheuzza at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 8

What I am actually trying to do is store this calculation inside an ActionListener

My code for this particular area as as follows:

if (e.getSource()==calcGrat) {

actGrat = Integer.parseInt(actualGratField.getText());

servicePeriod = Integer.parseInt(numServiceField.getText());

govtGratLimit = 350000;

lastDrawSalMain = Integer.parseInt(salField.getText());

lastDrawSal = lastDrawSalMain/12;

formulaTaxCalc = 15/26 * (lastDrawSal * servicePeriod);

if (actGrat < govtGratLimit) {

noGrat = actGrat;

} else if (actGrat < formulaTaxCalc) {

actGrat = noGrat;

} else if (govtGratLimit < actGrat) {

noGrat = govtGratLimit;

} else if (govtGratLimit < formulaTaxCalc) {

noGrat = govtGratLimit;

} else if (formulaTaxCalc < actGrat) {

noGrat = formulaTaxCalc;

} else if (formulaTaxCalc < govtGratLimit) {

noGrat = formulaTaxCalc;

}

taxableGrat = actGrat - noGrat;

I have been using If-else statements, but I am not getting the desired results. There seems to be a major flaw in this design.

Please have a look and let me know

Thank you

Abhay

Message was edited by:

abhaybhargav

abhaybhargava at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 9
please use the Math.min(int a, int b) to get the lowest.
Stone.lia at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 10
> please use the Math.min(int a, int b) to get the> lowest.That's only two numbers.
CaptainMorgan08a at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 11
huh,int n = Math.min(a,b);n = Math.min(n,c);Message was edited by: Stone.li
Stone.lia at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 12
> min(3,3,3);Wouldn't min(6,6,6) be a better example?
floundera at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 13
Thanks...Will try the Math.min thing....Abhay
abhaybhargava at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 14
But isnt it better If i used Double instead of int for numbers having decimals, etc....These are Financial Figures and they definitely will have Decimal calculations. Any views on this?
abhaybhargava at 2007-7-12 21:29:00 > top of Java-index,Java Essentials,Java Programming...
# 15

> But isnt it better If i used Double instead of int

> for numbers having decimals, etc....These are

> Financial Figures and they definitely will have

> Decimal calculations. Any views on this?

Yeah don't do that. Doubles are not good for financial figures.

cotton.ma at 2007-7-21 22:52:00 > top of Java-index,Java Essentials,Java Programming...
# 16
But will int values allow decimals and the like... Arent they whole numbers?
abhaybhargava at 2007-7-21 22:52:00 > top of Java-index,Java Essentials,Java Programming...
# 17
If you want to compare the two double to a crumb, you would transfer the double to the long and compare the long value.
Stone.lia at 2007-7-21 22:52:00 > top of Java-index,Java Essentials,Java Programming...
# 18
> But will int values allow decimals and the like...> Arent they whole numbers?You really need to read this. http://docs.sun.com/source/806-3568/ncg_goldberg.html
cotton.ma at 2007-7-21 22:52:00 > top of Java-index,Java Essentials,Java Programming...
# 19
Alright I will read it and check it out. Thanks
abhaybhargava at 2007-7-21 22:52:00 > top of Java-index,Java Essentials,Java Programming...