use dr java assp In this assignment you will write a program
use dr java assp
In this assignment, you will write a program that will ask the user for their birthday as two integers: a day and a month (in that order), and then it will tell them their astrological sign as well as their birthdate (details on the expected format of this output are below).
All input will be in integers. The program should tell the user their sign then write out the user\'s birthday in long-form (i.e. day: 17, month: 3 becomes March seventeenth). Finally, the program can optionally print an \"at-school appropriate\" horoscope.
If an incorrect date or month number is entered the program should print “error”.
For purposes of this program, you can assume all months end on the 31st, so you don\'t have to print \"error\" on Feb 30 or Sept 31 for example (nor do you have to worry about if the program is run on a leap year).
Notes about expected output:
The month will always be capitalized.
The day will always be lowercase.
Hyphens will be included for compound dates, such as the twenty-first, twenty-second, … , twenty-ninth, thirty-first.
Spelling of dates must be correct. For example note that: 8th is spelled eighth, 10th is spelled tenth, 20th is spelled twentieth and 30th is spelled thirtieth.
Use the following dates to determine the horoscope:
Aries 21 March – 19 April
Taurus 20 April – 20 May
Gemini 21 May – 20 June
Cancer 21 June – 22 July
Leo 23 July –22 August
Virgo 23 August – 22 September
Libra 23 September – 22 October
Scorpio 23 October – 21 November
Sagittarius 22 November – 21 December
Capricorn 22 December – 19 January
Aquarius 20 January – 18 February
Pisces 19 February – 20 March
Sample Run 1:
On what day of the month were you born? (number)
 25
 In which month were you born? (number)
 9
 Your sign is Libra
 Your birthday is: September twenty-fifth
 Horoscope: Expect lots of hard work, but also lots of laughs this month.
Sample Run 2:
On what day of the month were you born? (number)
 1
 In which month were you born? (number)
 13
 error
Sample Run 3:
On what day of the month were you born? (number)
 45
 In which month were you born? (number)
 6
 error
Solution
Ans.) import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.GregorianCalendar; import java.text.DateFormat; public class ZodiacSigns { public static void main(String[] args) { System.out.println(df.format(calendar.getTime())); int day = 0; int month = 0; int year = 0; while(true) { // Read in a date System.out.print(\"Enter the year: \"); year = readInt(); while(true){ System.out.print(\"Enter the month number January is 1, December is 12: \"); month = readInt(); if(validMonth(--month)) // Change month to zero-based and validate break; } while(true) { System.out.print(\"Enter the day in the month: \"); day = readInt(); if(validDay(day, month, year)) { // Verify date is valid for month and year break; } } // determine the sign calendar.set(year, month,day); // Set the calendar to the date entered // Match the year for the sign start dates for(int i = 0 ; i < signStartDates.length ; ++i) signStartDates[i].set(GregorianCalendar.YEAR, year); for(int i = 0 ; i < signStartDates.length ; ++i) if(calendar.after(signStartDates[i]) && calendar.before(signStartDates[(i+1)%signStartDates.length])) { System.out.println(df.format(calendar.getTime()) + \" is in the sign of \" + signs[i]); break; } // Try another date? System.out.println(\"Do you want to try another date(Enter Y or N)?\"); if(!yes()) { break; } } } // Validate the month value private static boolean validMonth(int month) { if(month >= 0 && month <= 11) return true; else System.out.println(\"The month value must be from 1 to 12. Try again.\"); return false; } // Validate the day value for the month and year private static boolean validDay(int day, int month, int year) { /* A valid day must be: - between 1 and 31 - less than 31 when the month is April, June, September, or November - less than 29 when the month is February and it is not a leap year - less than 30 when the month is February and it is a leap year */ if (day < 0 || day > 31) { System.out.println(\"Day values must be between 1 and 31. Try again.\"); return false; } if(day > 30 && (month == 3 || month == 5 || month == 8 || month == 10)) { System.out.println(\"Day values must be less than 31 when the month\" + \" is \" + MONTH_NAMES[month] + \". Try again.\"); return false; } if(day > 28 && month == 1 && !calendar.isLeapYear(year)) { System.out.println(year + \" is not a leap year so day values must be less than 29. Try again.\"); return false; } if(day > 29 && month == 1 && calendar.isLeapYear(year)) { return false; } return true; } // Reads an integer from the keyboard private static int readInt() { int value = 0; while(true) { try { value = Integer.parseInt(in.readLine().trim()); return value; }catch(NumberFormatException e) { System.out.println(\"Invalid input. Try again.\"); } catch(IOException e){ System.out.println(\"Error reading for the keyboard.\" + e.getMessage()); } } } private static boolean yes() { String str = null; while(true) { try { str = in.readLine().trim(); } catch(IOException e) { System.out.println(\"Error reading for the keyboard.\" + e.getMessage()); } if(str.equalsIgnoreCase(\"Y\")) { return true; } else if(str.equalsIgnoreCase(\"N\")) { break; } else { System.out.print(\"Invalid input. Try again. Enter Y or N: \"); } } return false; } private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Keyboard input // Names for Zodiac signs and start dates. Remember - months start at zero. private static String[] signs = {\"Aquarius\", \"Pisces\", \"Aries\" , \"Taurus\", \"Gemini\" , \"Cancer\", \"Leo\" , \"Virgo\", \"Libra\" ,\"Scorpio\", \"Sagittarius\", \"Capricorn\" }; private static GregorianCalendar[] signStartDates = { new GregorianCalendar(2002, 0, 20), // Aquarius start date new GregorianCalendar(2002, 1, 19), // Pisces start date new GregorianCalendar(2002, 2, 21), // Aries start date new GregorianCalendar(2002, 3, 20), // Taurus start date new GregorianCalendar(2002, 4, 21), // Gemini start date new GregorianCalendar(2002, 5, 21), // Cancer start date new GregorianCalendar(2002, 6, 23), // Leo start date new GregorianCalendar(2002, 7, 23), // Virgo start date new GregorianCalendar(2002, 8, 23), // Libra start date new GregorianCalendar(2002, 9, 23), // Scorpio start date new GregorianCalendar(2002, 10, 22), // Sagittarius start date new GregorianCalendar(2002, 11, 22), // Capricorn start date }; private static GregorianCalendar calendar = new GregorianCalendar(); // Array defining the names of the months. // Note that DateFormatSymbols class defines an instance method, getMonths(), // that will return exactly this array of String objects. private static final String[] MONTH_NAMES = { \"January\" , \"February\", \"March\" , \"April\", \"May\" , \"June\" , \"July\" , \"August\", \"September\", \"October\" , \"November\", \"December\" }; // Date formatter for displaying dates private static DateFormat df = DateFormat.getDateInstance(DateFormat.LONG); }

