Friday, April 24, 2009

Java Desktop Fortune teller

This is a simple java desktop application that "predicts" your fortune. It makes use of a getfortune method to display predefined fortune texts in a label. The getFortune method uses the random function from the "import java.lang.Math.*" package to choose the fortune to display.

fortune teller
public String getFortune() {
String prediction[]={"You will die a horrible death at 3:00 PM","You will win a lottery today","You will slip and fall today","Beware of red Vehicles","You will get a promotion today","You will become king one day","You will win the Nobel prize next year","You will win every contest you enter today","Your boss will go on leave for the next week","You will buy a car soon","You will become God"};
int forval=(int)Math.round(Math.random()*10);
return prediction[forval];
}

The different fortunes are stored in an array of strings and the value obtained from the random function is used as the array index. The value from random is between 0 and 1, and is multiplied by 10 to get the final value. Hence, the array will not go out of bound.

No comments: