1 Packagelearnexercise;2 3 ImportJava.util.Scanner;4 5 Public classPrintcalender {6 7 /**8 * @paramargs9 */Ten Public Static voidMain (string[] args) { One //TODO auto-generated Method Stub AScanner input=NewScanner (system.in); - -System.out.print ("Enter full Year (e.g.,2001):"); the intYear=input.nextint (); - -System.out.print ("Enter month as number between 1 and 12:"); - intMonth=input.nextint (); + - Printmonth (year,month); + A } at - Private Static voidPrintmonth (intYearintmonth) { - //TODO auto-generated Method Stub - Printmonthtitle (year, month); - Printmonthbody (year, month); - } in - Public Static voidPrintmonthtitle (intYearintmonth) { toSystem.out.println ("" +getmonthname (month) + "" +Year ); +System.out.println ("-------------------------"); -System.out.println ("Sun Mont Tue Wed Thu Fri Sat"); the * } $ Panax Notoginseng //Print Month body - Public Static voidPrintmonthbody (intYearintmonth) { the intstartday=Getstartday (year, month); + intNumberofdaysinmonth=Getnumberofdaysinmonth (year, month); A for(inti=0;i<startday;i++) theSystem.out.print (""); + for(inti=1;i<=numberofdaysinmonth;i++){ -System.out.printf ("%4d", i); $ if((I+startday)%7==0) $ System.out.println (); - } - System.out.println (); the } - Wuyi Public StaticString Getmonthname (intmonth) { theString monthname= ""; - Switch(month) { Wu Case1:monthname= "January"; Break; - Case2:monthname= "February"; Break; About Case3:monthname= "March"; Break; $ Case4:monthname= "April"; Break; - Case5:monthname= "may"; Break; - Case6:monthname= "June"; Break; - Case7:monthname= "July"; Break; A Case8:monthname= "August"; Break; + Case9:monthname= "September"; Break; the Case10:monthname= "October"; Break; - Case11:monthname= "Nobember"; Break; $ Case12:monthname= "December"; Break; the } the returnMonthName; the } the //gets the first day of the specified month is the week - Public Static intGetstartday (intYearintmonth) { in Final intStart_day_for_jan_1_1800=8; the inttotalnumberofdays=gettotalnumberofdays (year,month); the About return(totalnumberofdays+start_day_for_jan_1_1800)%7; the } the //returns the total number of days, calculated from January 1, 1800 until the day before the specified month of 1st the Public Static intGettotalnumberofdays (intYearintmonth) { + intTotal=0; - for(inti=1800;i<year;i++) the if(Isleapyear (i))Bayitotal=total+366; the Else thetotal=total+365; - - for(inti=1;i<month;i++) thetotal=total+Getnumberofdaysinmonth (year,i); the the returnTotal ; the } - the //returns the number of days in a specified month the Public Static intGetnumberofdaysinmonth (intYearintmonth) { the intResult=0;94 Switch(month) { the Case1: the Case3: the Case5:98 Case7: About Case8: - Case10:101 Case12:102Result=31;103 Break;104 Case4: the Case6:106 Case9:107 Case11:108Result=30;109 Break; the Case2:111Result=isleapyear (year)? 29:28; the Break;113 } the returnresult; the } the 117 //determine if a leap year118 Public Static BooleanIsleapyear (intYear ) {119 return(year%400==0) | | (year%4==0&&year%100!=0); - }121 122}
Method abstraction is a neat, hierarchical form of program modularity. It is easier to write, debug, maintain, and modify a set of programs that are composed of concise methods than others.
When writing large programs, you can use a top-down or bottom-up approach, and do not write the entire program at once.
JAVA Print the specified month calendar