Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar.

Similar presentations


Presentation on theme: "CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar."— Presentation transcript:

1 CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar

2 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 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 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 1. Print the heading and a separation line 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: public static void main(String[] args) { // Preparation // Print a heading for the calendar // Print the body of the calendar }

6 Code PrintCalendar 0. Prepare those information int calendarWidth = 20; // 2 characters for each day, 7 days, and 6 spaces (2*7+6) Create a GregorianCalendar object to help GregorianCalendar date = new GregorianCalendar(); 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 int year = date.get(Calendar.YEAR); int dayOfWeek = date.get(Calendar.DAY_OF_WEEK) - 1; // Sunday is -1, Monday is 0, and so on Set width to 20

7 Code PrintCalendar 0. Prepare those information public static void main(String[] args) { // Preparation GregorianCalendar date = new GregorianCalendar(); date.set(Calendar.DATE, 1); int month = date.get(Calendar.MONTH); int year = date.get(Calendar.YEAR); int dayOfWeek = date.get(Calendar.DAY_OF_WEEK) – 1; int calendarWidth = 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: private static void printHeading( 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 int precedingSpaces = (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 private static void printHeading( int month, int year, int width ) { // Print first line final String[] MONTH_NAMES = {" January ", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String monthString = MONTH_NAMES[month]; int precedingSpaces = (width - (monthString.length() + Integer.toString(year).length() + 1))/2; for(int i=0; i<precedingSpaces; i++) System.out.print(" "); System.out.println(monthString + " " + year); // Print a line for(int i=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 public static void main(String[] args) { // Preparation GregorianCalendar date = new GregorianCalendar(); date.set(Calendar.DATE, 1); int month = date.get(Calendar.MONTH); int year = date.get(Calendar.YEAR); int dayOfWeek = date.get(Calendar.DAY_OF_WEEK) – 1; int calendarWidth = 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? private static void printDays( int dayOfWeekForFirstDay, int daysInMonth) { for(int i=0; i<dayOfWeekForFirstDay; i++) System.out.print(" "); } 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

14 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 What is the last day? private static void printDays( int dayOfWeekForFirstDay, int daysInMonth) { for(int i=0; i<dayOfWeekForFirstDay; i++) System.out.print(" "); for(int day=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 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 What is the last day? Use another helper method to find how many days in a month private static int daysInMonth( int month, int year){ int numberOfDays; 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){ case 4: // April case 6: // June case 9: // September case 11: // November numberOfDays = 30; break; } return numberOfDays; }

16 public static void main(String[] args) { // Preparation GregorianCalendar date = new GregorianCalendar(); date.set(Calendar.DATE, 1); int month = date.get(Calendar.MONTH); int year = date.get(Calendar.YEAR); int dayOfWeek = date.get(Calendar.DAY_OF_WEEK) – 1; int calendarWidth = 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


Download ppt "CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar."

Similar presentations


Ads by Google