Saturday, October 13, 2012

[Android] Random Generator of a String in a String Array


When I came to live to this country, I tried to convince myself to not eat the same things all over again. There are many places/options available, so Why would I end going to McD’s again? Yes, junk food is inevitable in this country; and when a person gets in the boring cycle of routine, he/she will eventually end eating in the first restaurant that gets available. Sometimes the place will be healthy, and sometimes it won’t; but the thing is that we, “humans”, are creatures of habit. Eventually, we are going to get comfortable around the things that were unacceptable once.

Nowadays, it is really easy to find a restaurant to eat. There are many apps available in multiple platforms that will help people to find these places. For example, using several conditions, Urbanspoon (Android, Blackberry, iOS) will help the user to find the right one. Nevertheless, because we are creatures of habit (there are other conditions that apply of course: willing to drive to a far place, amount of time, money available, special occasion, etc.), we are going to end in the same restaurants over and over again.

Here is when my girlfriend appears. She, as everyone else, has her own tastes, but the problem is that I like everything; (Sometimes, I see myself as the starving goat from an old Tex Avery cartoon; --> Billy Boy) and, we don’t easily agree to eat in any of our favorite restaurants. In that precise moment, Java came to help :) and with the assistance of a String[] and a random generator, the problem was solved. (Yes, you are right; McDonald’s is inside of the array).

Here is the code:

Some screenshots of the app in action:





Thursday, October 11, 2012

Controlling Threads in Java

As you may know, Threads are really important in programming. Without them programs couldn't perform important tasks at the same time. An example of a Thread that everyone is familiar with is the Buffering/Progress Bar in all video-related websites like YouTube. The programmer is able to control them with different tools such as Synchronization, wait(), notify(), and sleep() methods.

In the next example I use a variation from a Youtube Tutorial made by batwingd. I made the program User Programmable; The user is able to change the speed, and change the resting time of two animals that are going to compete in a race.

The program is divided in three classes: The main class, the Race Class, and The Animal Class:
  Output:
Please enter rabbit's name:
Bugs
Your rabbit's name is Bugs
Please set Bugs's time for rest (0-1500 milliseconds):
1000
Please set Bugs's speed (0-5):
3
Please enter turtle's name:
Don
Your turtle's name is Don
Please set Don's time for rest (0-1500 milliseconds):
1500
Please set Don's speed (0-5):
5
On your marks...
Get ready...
GO!
Don is off!
Bugs is off!
Bugs stopped to rest for 0.506 seconds in the yard number 3
Don stopped to rest for 0.106 seconds in the yard number 5
Bugs stopped to rest for 0.907 seconds in the yard number 6
Don stopped to rest for 0.314 seconds in the yard number 10
Bugs stopped to rest for 0.864 seconds in the yard number 9
Bugs stopped to rest for 0.721 seconds in the yard number 12
Don stopped to rest for 0.671 seconds in the yard number 15
Bugs stopped to rest for 0.448 seconds in the yard number 15
Don stopped to rest for 0.783 seconds in the yard number 20
Bugs stopped to rest for 0.584 seconds in the yard number 18
Bugs stopped to rest for 0.88 seconds in the yard number 21
Don stopped to rest for 0.118 seconds in the yard number 25
Bugs stopped to rest for 0.553 seconds in the yard number 24
Don stopped to rest for 0.515 seconds in the yard number 30
Bugs stopped to rest for 0.92 seconds in the yard number 27
Bugs stopped to rest for 0.024 seconds in the yard number 30
Don stopped to rest for 0.19 seconds in the yard number 35
Bugs stopped to rest for 0.798 seconds in the yard number 33
Don stopped to rest for 0.319 seconds in the yard number 40
Bugs stopped to rest for 0.013 seconds in the yard number 36
Bugs stopped to rest for 0.634 seconds in the yard number 39
Don stopped to rest for 0.997 seconds in the yard number 45
Bugs stopped to rest for 0.216 seconds in the yard number 42
Don stopped to rest for 0.993 seconds in the yard number 50
Bugs stopped to rest for 0.425 seconds in the yard number 45
Bugs stopped to rest for 0.847 seconds in the yard number 48
Don stopped to rest for 0.947 seconds in the yard number 55
Bugs stopped to rest for 0.406 seconds in the yard number 51
Don stopped to rest for 0.202 seconds in the yard number 60
Bugs stopped to rest for 0.725 seconds in the yard number 54
Bugs stopped to rest for 0.725 seconds in the yard number 57
Don stopped to rest for 0.192 seconds in the yard number 65
Bugs stopped to rest for 0.119 seconds in the yard number 60
Don stopped to rest for 1.125 seconds in the yard number 70
Bugs stopped to rest for 0.733 seconds in the yard number 63
Bugs stopped to rest for 0.756 seconds in the yard number 66
Don stopped to rest for 1.416 seconds in the yard number 75
Bugs stopped to rest for 0.845 seconds in the yard number 69
Don stopped to rest for 1.435 seconds in the yard number 80
Bugs stopped to rest for 0.999 seconds in the yard number 72
Bugs stopped to rest for 0.51 seconds in the yard number 75
Don stopped to rest for 0.592 seconds in the yard number 85
Bugs stopped to rest for 0.292 seconds in the yard number 78
Don stopped to rest for 0.389 seconds in the yard number 90
Bugs stopped to rest for 0.326 seconds in the yard number 81
Bugs stopped to rest for 0.696 seconds in the yard number 84
Don stopped to rest for 0.183 seconds in the yard number 95
Bugs stopped to rest for 0.327 seconds in the yard number 87
Don stopped to rest for 0.516 seconds in the yard number 100
>>>Don ended at position 1! He is the winner!
Bugs stopped to rest for 0.505 seconds in the yard number 90
Bugs stopped to rest for 0.565 seconds in the yard number 93
Bugs stopped to rest for 0.633 seconds in the yard number 96
Bugs stopped to rest for 0.437 seconds in the yard number 99
Bugs stopped to rest for 0.393 seconds in the yard number 102
>>>Bugs ended at position 2 He lost!