I am implementing a class called GregorianCalendar. However, I am stuck on the method toString(). When I run my code has i get the output:
1, March 19, 2012 occurred in a leap year. I need the '1' to convert to the day, 'Monday'.
package gregoriandatetesterapp; /** * * @author Jarrod */ public class GregorianDate { /** * integer representing the day */ private int day; /** * integer 1-12 representing the month */ private int month; /** * integer representing the year */ private int year; /** * creates a date object that represents January 1, 1970 */ public GregorianDate() { month = 1; day = 1; year = 1970; } /** * creates a date object. * @param mm - the month of the specified date * @param dd - the day of the specified date * @param yy - the year of the specified date * @throw java.lang.IllegalArgumentException - when date is not valid */ public GregorianDate(int mm, int dd, int yy) { if (mm < 1 || mm > 12) throw new IllegalArgumentException("This is not a valid month"); if (dd < 1 || dd > 31) throw new IllegalArgumentException("This is not a valid day"); if ((mm == 4 || mm == 6 || mm == 10 || mm == 11) && dd == 31) throw new IllegalArgumentException("This is not a valid date"); if (mm == 2 && isLeap() && dd > 28) throw new IllegalArgumentException("This is not a valid date"); if (mm == 2 && dd > 28) throw new IllegalArgumentException("This is not a valid date"); month = mm; day = dd; year = yy; } /** * gives a number representing the day of the week of this date * @return a number 0-6 representing the day of the week on which this date occurs. 0 = Sunday, 1 = Monday, etc */ public int dayOfWeek() { int u = 2*(3-((year/100)%4)); int v = year%100; int w = v/4; int x = 0; if (isLeap() == false) { if (month == 1) x = 0; if (month == 2) x = 2; if (month == 3 || month == 11) x = 3; if (month == 4 || month == 7) x = 6; if (month == 5) x = 1; if (month == 6) x = 4; if (month == 8) x = 2; if (month == 9 || month == 12) x = 5; if (month == 10) x = 0; } else { if (month == 1) x = 6; if (month == 2) x = 2; if (month == 3 || month == 11) x = 3; if (month == 4 || month == 7) x = 6; if (month == 5) x = 1; if (month == 6) x = 4; if (month == 8) x = 2; if (month == 9 || month == 12) x = 5; if (month == 10) x = 0; } int y = u + v + w + x + day; return y%7; } /** * determine whether a year is a leap year. */ public boolean isLeap() { if (year%400 == 0 || (year%100 != 0 && year%4 == 0)) return true; else return false; } /** * gives the month in words * @param monthNum - a number 1-12 representing a month * @return the month in words with only its first letter in uppercase. */ public java.lang.String monthToString(int monthNum) { month = monthNum; if (monthNum == 1) return "January"; if (monthNum == 2) return "February"; if (monthNum == 3) return "March"; if (monthNum == 4) return "April"; if (monthNum == 5) return "May"; if (monthNum == 6) return "June"; if (monthNum == 7) return "July"; if (monthNum == 8) return "August"; if (monthNum == 9) return "September"; if (monthNum == 10) return "October"; if (monthNum == 11) return "November"; if (monthNum == 12) return "December"; else return "Invalid Month"; } /** * gives the day of the week in words */ public java.lang.String weekdayToString(int dayNum) { day = dayNum; if (dayNum == 0) return "Sunday"; if (dayNum == 1) return "Monday"; if (dayNum == 2) return "Tuesday"; if (dayNum == 3) return "Wednesday"; if (dayNum == 4) return "Thursday"; if (dayNum == 5) return "Friday"; if (dayNum == 6) return "Saturday"; else return "Invalid Day"; } /** * gives a string representation of this object */ public java.lang.String toString() { String ds = new Integer(dayOfWeek()).toString(); ds = ds + ", "; ds = ds + monthToString(month); ds = ds + day; ds = ds + ", "; ds = ds + year; return ds; } }
package gregoriandatetesterapp; import java.util.*; import java.lang.*; public class GregorianDateTester { public static void main(String[] args) { int month, day, year; Scanner keyb = new Scanner(System.in); System.out.print("Enter numeric values for the month, day and year> "); month = keyb.nextInt(); day = keyb.nextInt(); year = keyb.nextInt(); GregorianDate gd = new GregorianDate(month, day, year); if (gd.isLeap()) System.out.println(gd + " occurred in a leap year."); else System.out.println(gd + " occurred in a non-leap year."); } }
So output is currently looking like this:
Enter numeric values for month, day and year of a date> 3 20 2012
2, March 20, 2012 occurred in a leap year.
I need output to look like this:
Enter numeric values for month, day and year of a date> 3 20 2012
Tuesday, March 20, 2012 occurred in a leap year.