1 / 17

CSc2310 tutoring session, week 8 Fall, 2012

CSc2310 tutoring session, week 8 Fall, 2012. Code PrintCalendar. Haidong Xue. 5:30pm—8:30pm 10/30/2012 and 10/31/2012. CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: 1. Code PrintCalendar

Download Presentation

CSc2310 tutoring session, week 8 Fall, 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSc2310 tutoring session, week 8Fall, 2012 • Code PrintCalendar HaidongXue 5:30pm—8:30pm 10/30/2012 and 10/31/2012

  2. CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue • Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: 1. Code PrintCalendar 2. Q&A • Answer your questions about java programming

  3. Code PrintCalendar • Problem definition Print a calendar for the current month, e.g.: October 2012 -------------------- 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

  4. Code PrintCalendar 1. Print the heading and a separation line • Design October 2012 -------------------- 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 2. Print the body Before printing, some information is necessary: What is the current month, year? What is the day of week of the first day? What is the width of the calendar? 0. Prepare those information

  5. Code PrintCalendar Create a new class named PrintCalendar In the main method, set a frame like: publicstaticvoid main(String[] args) { // Preparation // Print a heading for the calendar // Print the body of the calendar }

  6. Code PrintCalendar 0. Prepare those information Create a GregorianCalendar object to help • GregorianCalendar date = newGregorianCalendar(); • date.set(Calendar.DATE, 1); // Adjust to first day of month Get information from Gregorian Calendar information • int month = date.get(Calendar.MONTH); // 0 means Jan, 1 means Feb, and so on • intyear = date.get(Calendar.YEAR); • intdayOfWeek = date.get(Calendar.DAY_OF_WEEK) - 1; // Sunday is -1, Monday is 0, and so on Set width to 20 intcalendarWidth = 20; // 2 characters for each day, 7 days, and 6 spaces (2*7+6)

  7. Code PrintCalendar 0. Prepare those information publicstaticvoid main(String[] args) { // Preparation GregorianCalendar date = newGregorianCalendar(); date.set(Calendar.DATE, 1); int month = date.get(Calendar.MONTH); intyear = date.get(Calendar.YEAR); intdayOfWeek = date.get(Calendar.DAY_OF_WEEK) – 1; intcalendarWidth = 20; // Print a heading for the calendar // Print the body of the calendar }

  8. Code PrintCalendar 1. Print the heading and a separation line October 2012 -------------------- 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 • How many spaces at the beginning? • What is the string for the month? To keep is organized, we can finish it in another method: privatestaticvoidprintHeading( int month, int year, int width )

  9. Code PrintCalendar 1. Print the heading and a separation line • Month String final String[] MONTH_NAMES = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String monthString = MONTH_NAMES[month]; • Number of preceding spaces intprecedingSpaces = (width - (monthString.length() + Integer.toString(year).length() + 1))/2; October 2012 -------------------- 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

  10. Code PrintCalendar 1. Print the heading and a separation line privatestaticvoidprintHeading( int month, int year, int width ) { // Print first line finalString[] MONTH_NAMES = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String monthString = MONTH_NAMES[month]; intprecedingSpaces = (width - (monthString.length() + Integer.toString(year).length() + 1))/2; for(inti=0; i<precedingSpaces; i++) System.out.print(" "); System.out.println(monthString+ " " + year); // Print a line for(inti=0; i<width; i++) System.out.print("-"); System.out.println(); } Month string First line Separation line

  11. Code PrintCalendar 1. Print the heading and a separation line publicstaticvoid main(String[] args) { // Preparation GregorianCalendar date = newGregorianCalendar(); date.set(Calendar.DATE, 1); int month = date.get(Calendar.MONTH); intyear = date.get(Calendar.YEAR); intdayOfWeek = date.get(Calendar.DAY_OF_WEEK) – 1; intcalendarWidth = 20; // Print a heading for the calendar printHeading(month, year, calendarWidth); // Print the body of the calendar } October 2012 -------------------- Run it, you should see:

  12. Code PrintCalendar 2. Print the body October 2012 -------------------- 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 How many columns before the first day? What is the last day?

  13. Code PrintCalendar 2. Print the body (Start from Sunday) October 2012 -------------------- 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 How many columns before the first day? The number is equal to the index of the first day of week, e.g. the first day of week is Monday, the index is 1, there is 1 column before it privatestaticvoidprintDays( intdayOfWeekForFirstDay, intdaysInMonth) { for(inti=0; i<dayOfWeekForFirstDay; i++) System.out.print(" "); }

  14. Code PrintCalendar October 2012 -------------------- 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 What is the last day? 2. Print the body privatestaticvoidprintDays( intdayOfWeekForFirstDay, intdaysInMonth) { for(inti=0; i<dayOfWeekForFirstDay; i++) System.out.print(" "); for(intday=1; day<=daysInMonth; day++) { if(day<=9) System.out.print(" "); // for 1 digit days System.out.print( day + " "); if((day+dayOfWeekForFirstDay)%7==0) System.out.println(); //start a new line after Sat } }

  15. Code PrintCalendar October 2012 -------------------- 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 What is the last day? 2. Print the body Use another helper method to find how many days in a month privatestaticintdaysInMonth( int month, int year){ intnumberOfDays; if(month==1) // February { numberOfDays= 28; if(year % 4 == 0){ numberOfDays= 29; if(year % 100 == 0 && year % 400 != 0) numberOfDays= 28; } } else// others{ numberOfDays= 31; switch(month){ case4: // April case6: // June case9: // September case11: // November numberOfDays= 30; break; } } returnnumberOfDays; }

  16. publicstaticvoid main(String[] args) { // Preparation GregorianCalendar date = newGregorianCalendar(); date.set(Calendar.DATE, 1); int month = date.get(Calendar.MONTH); intyear = date.get(Calendar.YEAR); intdayOfWeek = date.get(Calendar.DAY_OF_WEEK) – 1; intcalendarWidth = 20; // Print a heading for the calendar printHeading(month, year, calendarWidth); // Print the body of the calendar printDays(dayOfWeek, daysInMonth(month, year)); } October 2012 -------------------- 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 Finished code can be found from: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/code/PrintCalendar.java Try it and let me know your questions!

  17. Please let me know your questions. I will be here till 8:30pm

More Related