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

CSc2310 tutoring session, week 8 Fall, 2012

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: CSc2310 tutoring session, week 8 Fall, 2012

CSc2310 tutoring session, week 8Fall, 2012

Haidong Xue

5:30pm—8:30pm10/30/2012 and 10/31/2012

- Code PrintCalendar

Page 2: CSc2310 tutoring session, week 8 Fall, 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

2. Q&A- Answer your questions about java programming

Page 3: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar

• Problem definitionPrint 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

Page 4: CSc2310 tutoring session, week 8 Fall, 2012

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

Page 5: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendarCreate a new class named PrintCalendarIn 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}

Page 6: CSc2310 tutoring session, week 8 Fall, 2012

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

Page 7: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar0. Prepare those informationpublic 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

}

Page 8: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar1. 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 )

Page 9: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar1. Print the heading and a separation line • Month Stringfinal String[] MONTH_NAMES = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String monthString = MONTH_NAMES[month];

• Number of preceding spacesint 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

Page 10: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar1. 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

Page 11: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar1. 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:

Page 12: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar2. 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?

Page 13: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar2. 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

Page 14: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar2. 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 }}

Page 15: CSc2310 tutoring session, week 8 Fall, 2012

Code PrintCalendar2. 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 monthprivate 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;}

Page 16: CSc2310 tutoring session, week 8 Fall, 2012

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!

Page 17: CSc2310 tutoring session, week 8 Fall, 2012

Please let me know your questions.

I will be here till 8:30pm