CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar
CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: There are 2 sections: 1. Code PrintCalendar 2. Q&A -Answer your questions about java programming
Code PrintCalendar Problem definition Print a calendar for the current month, e.g.: October
Code PrintCalendar Design October 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
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 }
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
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 }
Code PrintCalendar 1. Print the heading and a separation line October 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 )
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
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
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 Run it, you should see:
Code PrintCalendar 2. Print the body October How many columns before the first day? What is the last day?
Code PrintCalendar 2. Print the body (Start from Sunday) October 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
Code PrintCalendar 2. Print the body October 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 }
Code PrintCalendar 2. Print the body October 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; }
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 Finished code can be found from: Try it and let me know your questions!
Please let me know your questions. I will be here till 8:30pm