Вы находитесь на странице: 1из 6

Calendar.

java package calendar; public class Calendar { private int birthmonth, birthday, birthweek, dayofthanksgiving; private String birthmonthname; private final int year; public Calendar(String birthmonthname, int bd, int y){ this.birthmonth= getMonthnum (birthmonthname); birthday=bd; year=y; } public static int getMonthnum(String birthmonthname){ if(birthmonthname.equals("january")){ return(1); } if(birthmonthname.equals("february")){ return(2); } if(birthmonthname.equals("march")){ return(3); } if(birthmonthname.equals("april")){ return(4); } if(birthmonthname.equals("may")){ return(5); } if(birthmonthname.equals("june")){ return(6); } if(birthmonthname.equals("july")){ return(7); } if(birthmonthname.equals("august")){ return(8); } if(birthmonthname.equals("september")){ return(9); } if(birthmonthname.equals("october")){ return(10); Page 1

Calendar.java } if(birthmonthname.equals("november")){ return(11); } else{ return(12); }

} public String getMonth(int birthmonth){ switch(birthmonth){ case 1:{ return("January"); } case 2:{ return("February"); } case 3:{ return("March"); } case 4:{ return("April"); } case 5:{ return("May"); } case 6:{ return("June"); } case 7:{ return("July"); } case 8:{ return("August"); } case 9:{ return("September"); } case 10:{ return("October"); } Page 2

Calendar.java case 11:{ return("November"); } default:{ return("December"); } } } public String setBirthday(){ return("You were born on "+getMonth(birthmonth)+" "+birthday+", "+ year+" which is a "+getWeekDay(birthmonth, birthday,year)+"."); } public String setThanksgiving(){ int offset = 0; String day=getWeekDay(11, 1,year); if (day.equals("Thursday")){ offset =1; } if (day.equals("Monday")){ offset=4; } if(day.equals("Tuesday")){ offset=3; } if(day.equals("Wednesday")){ offset=2; } if(day.equals("Friday")){ offset=6; } if(day.equals("Saturday")){ offset=5; } return("Thanksgiving is on November "+(21+offset)+", "+year+" which is on a Thursday." ); } public String getWeekDay(int month, int day, int year){ int firsttwoo = year/100; int lasttwoo = year % 100; Page 3

Calendar.java int a = 2 * (3 - (firsttwoo % 4)); int b = lasttwoo + (lasttwoo / 4); int c; switch (month) { case 1: { c = 0; break; } case 2: { c = 3; break; } case 3: { c = 3; break; } case 4: { c = 6; break; } case 5: { c = 1; break; } case 6: { c = 4; break; } case 7: { c = 6; break; } case 8: { c = 2; break; } case 9: { c = 5; break; } case 10: { c = 0; Page 4

Calendar.java break; } case 11: { c = 3; break; } case 12: { c = 5; break; } default: return "error"; } int d=day; int sum=a+b+c+d; int remainder= sum%7; switch(remainder){ case 0:{ return("Sunday"); } case 1:{ return("Monday"); } case 2:{ return("Tuesday"); } case 3:{ return("Wednesday"); } case 4:{ return("Thursday"); } case 5:{ return("Friday"); } default: return("Saturday"); } } public String setEaster(){ int a=year%19; int b=year%4; Page 5

Calendar.java int c=year%7; int d=(19*a+24)%30; int e=(2*b+4*c+6*d+5)%7; int f=22+d+e; if(f<=31){ return("Easter is on March "+f+year+", and it falls on a "+getWeekDay(3,f,year)+"."); } return ("Easter is on April "+(f-31)+", "+year+" which falls on a "+getWeekDay(4,(f-31),year)+".");

} public String setNewyears(){ return("New Years is January 1st, "+year+" and it is on a "+ (getWeekDay(1,1,year)+".") ); } public String setHalloween(){ return("Halloween is on October 31st, "+year+" and it is on a "+(getWeekDay(10, 31,year)+".")); } public String getEverything(){ return(setBirthday()+"\n"+setEaster()+"\n"+setThanksgiving() +"\n"+setNewyears()+"\n"+setHalloween() ); } public String toString(){ return(getEverything()); } }

Page 6

Вам также может понравиться