The following Java program shows the Zodiac sign corresponding to a given date. This little program can be used to find your Zodiac sign if you input your date of birth.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | // Ouputs the Zodiac sign corresponding to a given date. import java.util.*; import java.text.DateFormatSymbols; class TryCalendar { public static void main(String[] args) { String[] zodiac = { "CAPRICORN", "AQUARIUS", "PISCES", "ARIES", "TAURUS", "GEMINI", "CANCER", "LEO", "VIRGO", "LIBRA", "SCORPIO", "SAGITTARIUS" }; // Dates for the start of each sign period, starting with Aquarius GregorianCalendar[] signStartDates = { new GregorianCalendar(2000, Calendar.JANUARY, 21), new GregorianCalendar(2000, Calendar.FEBRUARY, 20), new GregorianCalendar(2000, Calendar.MARCH, 21), new GregorianCalendar(2000, Calendar.APRIL, 21), new GregorianCalendar(2000, Calendar.MAY, 22), new GregorianCalendar(2000, Calendar.JUNE, 22), new GregorianCalendar(2000, Calendar.JULY, 24), new GregorianCalendar(2000, Calendar.AUGUST, 24), new GregorianCalendar(2000, Calendar.SEPTEMBER, 24), new GregorianCalendar(2000, Calendar.OCTOBER, 24), new GregorianCalendar(2000, Calendar.NOVEMBER, 23), new GregorianCalendar(2000, Calendar.DECEMBER, 22) }; // Get the calendar for a birthday GregorianCalendar birthday = getDate(); String yourSign = zodiac[0]; // Set CAPRICORN as default for (int i = 0; i0 && month < 13 && day > 0 && day <= monthDays[month - 1]) return new GregorianCalendar(2000, month - 1, day); System.out.println("Invalid date entered. Try again."); } System.out.println("Five invalid dates entered. Sorry - no more tries..."); System.exit(1); return null; } private static int[] monthDays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; private static FormattedInput in = new FormattedInput(); } //****************************************** //FormattedInput.java //****************************************** import java.io.*; public class FormattedInput implements Serializable { public String stringRead() { try { for (int i = 0; i < 5; i++) { int tokenType = tokenizer.nextToken(); if (tokenType == tokenizer.TT_WORD || tokenType == '\"') return tokenizer.sval; else if (tokenType == '!') return "!"; else { System.out.println("Incorrect input. Re-enter a string between double quotes"); continue; } } System.out.println("Five failures reading a string" + " - program terminated"); System.exit(1); return null; } catch (IOException e) { System.out.println(e); System.exit(1); return null; } } public int intRead() { try { for (int i = 0; i < 5; i++) { if (tokenizer.nextToken() == tokenizer.TT_NUMBER && tokenizer.nval == (double)((long) tokenizer.nval)) return (int) tokenizer.nval; else { System.out.println("Incorrect input: " + tokenizer.sval + " Re-enter an integer"); continue; } } System.out.println("Five failures reading an int value" + " - program terminated"); System.exit(1); return 0; } catch (IOException e) { System.out.println(e); System.exit(1); return 0; } } private StreamTokenizer tokenizer = new StreamTokenizer(new InputStreamReader(System.in)); } |