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

Slides:



Advertisements
Similar presentations
Months of the year December January November October February
Advertisements

Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Chubaka Producciones Presenta :.
The Months and The Seasons Prepared by Claudia Doria and Terra Myers.
2012 CALENDAR. JANUARY 2012 Sunday 日 Monday 月 Tuesday 火 Wednesday 水 Thursday 木 Friday 金 Saturday 土
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Displaying a calendar A worked example From Liang, Example 4.6, Pp
January 2012 Monday Tuesday Wednesday Thursday Friday Sat/ Sun / /8 14/15 21/22 28/
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Chicas, este calendario si es pa' nosotras !!!!!.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAY WEEK WEEK WEEK WEEK WEEK CALENDAR PROJECT.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2008 Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need.
CSc2310 tutoring session, week 7 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -Using arrays -Case study: PhoneDirectory.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 11/27/2012 and 11/28/2012 -Class Members vs Instance Members.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
The 5 th and 6 th tutoring session - A case study about verifying a ISBN - Writing code for problems in HW1 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
Months of the Year Macarena song.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Calendar for 2011 Months of the Year with Holidays.
The 9 th and 10 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/2/2012 and 10/3/2012 -Review loop structures and improve CheckISBN and CourseAverage.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/13/2012 and 11/14/2012 -Dr Cao’s Assignment 5 -i.e. Problem 4 on page 335 and.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Chapter 8: More Control Structures Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 8 More Control.
The Calendar.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Deadline for Requisitions Payment Processing Date
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
13-block rotation schedule
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
Logo Calendar – January 2012 TO DO LIST 01/04/2012 Example TO DO LIST
January MON TUE WED THU FRI SAT SUN
2017/18 Payment Calendar Due Date Cut-off Day 1st of every month
ANNUAL CALENDAR HOLIDAYS JANUARY FEBRUARY MARCH APRIL MAY JUNE
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
McDonald’s Kalender 2009.
2300 (11PM) September 21 Blue line is meridian..
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
McDonald’s calendar 2007.
1 - January - Sun Mon The Wed Thu Fri Sat
Proud As A Peacock! We are very proud of__________________
Teacher name August phone: Enter text here.
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Calendar.
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
February 2007 Note: Source:.
JUNE 2010 CALENDAR PROJECT PLANNING 1 Month MONDAY TUESDAY WEDNESDAY
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
McDonald’s calendar 2007.
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

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