Thursday, January 22, 2015

I call this simple program, Zodiac Finder, because its simply generate your animal birth sign and zodiac sign by entering your birth date with this format (MMDDYYY).

This simple program composed of 1 and 2 dimensional array, for loops statement, while statement, and conditional statement.

here a we go, take a look and try to evaluate the code and feel free to comment and suggest.


package teamUndefined;

import java.io.*;

public class ZodiacSign {

public static void main(String args[]) throws IOException{

BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

/*since month, year sign or animal birth sign and also a zodiac sign is a static variables, i decided to put it on array because it doesn't change in the  run-time*/

String[] _month = {"January", "February", "March", "April",  "May", "June", "July", "August",  "September", "October","November", "December"};  

String[] yearSign = {"rat", "ox", "tiger","rabbit", "dragon","snake","horse", "goat", "monkey","rooster","dog", "pig"};

String[] zodiacSign = {"Aries", "Taurus", "Gemeni", "Cancer", "Leo","Virgo", "Libra",    "Scorpio","Sagittarius", "Capricorn", "Aquarius", "Pisces"};

/*Now, lets store the different ranges of dates according to its sign, i concluded to store it in two dimensional arrays for me two easily track or identify the user birth date range, e.g if user enter 050721967, automatically and obviously the birth month is 05 = May, 07 =Day 07, now if you converted May 07, the output is 507 in integer form, so, i can easily compare and identify using IF statement where the birth date belong to*/

String[][] _dates =  {
{"321","419"},//aries
{"420","520"},//taurus
{"521","620"},//gemeni
{"621","722"},//cancer
{"723","822"},//leo
{"823","922"},//virgo
{"923","1022"},//libra
{"1023","1121"},//scorpio
{"1122","1221"},//Sagittarius
{"1222","119"},//Capricorn
{"120","218"},//Aquarius
{"219","320"}//Pisces
};

String x , _mo = null;
System.out.print("Enter BDate: " );
x = buff.readLine();

int _year = Integer.parseInt(x.substring(4,x.length())),
_day = Integer.parseInt(x.substring(2,4)),
_months = Integer.parseInt(x.substring(0,2)),
_moDay = Integer.parseInt(x.substring(0,2).concat(x.substring(2,4)));

/*lets filter the incorrect input of a user, in this while, statement you can do the same filter on Day and Year, since at the code below, it intended only for Month, you can add your own if you try it*/

while (Integer.parseInt(x.substring(0, 2)) > 12) {
System.out.print("Please enter correct birthdate\n");
System.out.print("Enter BDate: " );
x = buff.readLine();
}

int r = 11,  c = 2, _zodiacDates1 = 0, _zodiacDates2 = 0;

/*now lets iterate our two dimensional array under the zodiac sign ranges*/

for (int i = 0; i < r; i++) {

for (int j = 0; j < c; j++) {

/*and store those values in these two variables */

_zodiacDates2 = _zodiacDates1;
_zodiacDates1 = Integer.parseInt(_dates[i][j]) ;

}

/*this is IF statement is a crucial part, because you cant compare 1223 to 118, So, i get the range to 1231 from December, and the range for 119 for January, for me to get the TRUE answer, TRUE || FALSE = TRUE, TRUE && TRUE = TRUE, FALSE && FALSE = FALSE , FALSE || TRUE = TRUE*/
if (i == 9) {
if ((_moDay >= 1222 && _moDay <= 1231)  || (_moDay >= 101 && _moDay <= 119)) {
r = i;
break;
}
}
else{
if (_moDay >= _zodiacDates2 && _moDay <= _zodiacDates1) {
r = i;
}
}
}

//getting the months on array _months , and access the array index
for (int i = 0; i < _month.length; i++) {
if (i == (_months - 1)) {
_mo = _month[i];
break;
}
}

/*assigning year sign in specific year, since it is repeated in every 12 years, so this is my approach on how to sokved this*/

int  j = 0;
for ( int i = 1900; i <= _year; i++) {
if (j == yearSign.length) {
j = 0;
}
j++;
}
/*and now we can display the results*/
System.out.print("You were born on " + _mo + " " + _day + ","+ _year + "\nin the Year of the " + yearSign[j - 1] +"\nUnder the sign of " + zodiacSign[r]);

}

}

i think this is enough for the beginners to start there logic and create their own algorithm. I hope this code will help specially for the rookie coder, who start developing on java environment.
Newer Post
Previous
This is the last post.

0 comments:

Post a Comment