CS 112 Introduction to Programming - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS 112 Introduction to Programming

Description:

CS 112 Introduction to Programming ... Calculate the weekday of the 1 days of a month (in a specific year) static void IndentInitialWeekdays( int weekday ) ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 17
Provided by: Richar9
Category:

less

Transcript and Presenter's Notes

Title: CS 112 Introduction to Programming


1
CS 112 Introduction to Programming
  • Lecture 14
  • The Calendar Program
  • Http//zoo.cs.yale.edu/classes/cs112/

2
Outline
  • Admin. and review
  • Stepwise refinement using the Calendar program as
    an example

3
Admin.
  • Assignment 2 office hours
  • 1200 PM - 500 PM (DL 120)
  • 0230 PM 400 PM (AKW 202)
  • 0500 PM 630PM (DL 120)

4
Recap Variable Scope and Duration
  • Scope
  • A local variable is accessible after it is
    declared and before the end of the block
  • A class variable is accessible in the whole class
  • Parameter passing with ref and out makes some
    variables aliases of others
  • Duration
  • A local variable may exist but is not accessible
    in a method,
  • e.g., method A calls method B, then the local
    variables in method A exist but are not
    accessible in B.

5
Example
class Test const int NoOfTries 3 //
class scope static int Square ( int x ) //
formal arg. // NoOfTries and x in
scope int square x x // square local
var. // NoOfTries, x and square in scope
return square static int
AskForAPositiveNumber ( int x ) //
NoOfTries and x in scope for ( int i 0
i lt NoOfTries i ) // NoOfTries, i,
and x in scope string str
Console.ReadLine() // NoOfTries, i, x,
and str in scope int temp
Int32.Parse( str ) // NoOfTries, i, x,
str and temp in scope if (temp gt 0)
return temp // now only x and
NoOfTries in scope return 0 //
AskForPositiveNumber static void Main(
string args )
6
Review Program Development Process
7
Calendar Requirements
  • Get a year from the user, the earliest year
    should be 1900
  • Get a month from the user, the input should be
    from 0 to 12
  • If 0, print calendar for all 12 months
  • Otherwise, print the calendar of the month of the
    year

8
Design
year GetYearFromUser()
month GetMonthFromUser()
month ? 0
t
f
PrintMonth(month, year)
PrintYear(year)
9
PrintMonth( month, year )
  • January 1900
  • Sun Mon Tue Wed Thu Fri Sat
  • 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
Design PrintMonth( month, year )
Print month header
GetFirstWeekday(month, year)
IndentInitialWeekdays
GetMonthDays(month, year)
Print each day
11
Design PrintMonth( month, year )
Print month header
GetFirstWeekday (month, year)
IndentInitialWeekdays
GetMonthDays(month, year)
Print each day
12
Implementation
  • Important
  • Implement simple methods first
  • Test a method
  • using some dummy method calls, or
  • e.g, Call the method in Main() with some typical
    test cases
  • using Visual Studio Immediate window

13
Methods To Be Implemented and an Order to
Implement them
  • static string GetMonthName( int month )
  • // The English word for a specific month
  • static bool IsLeapYear( int month, int year )
  • // Check if a year is a leap year
  • static int GetMonthDays( int month, int year )
  • // Calculate the number of days for a specific
    month of a specific year
  • static int GetFirstWeekday( int month, int year
    )
  • // Calculate the weekday of the 1 days of a
    month (in a specific year)
  • static void IndentInitialWeekdays( int weekday )
  • // Indenting the first line so that the
    first day appears in the correct position
  • static void PrintMonth( int month, int year )
  • static void PrintYear( int month, int year )
  • static void GievInstructions( int month, int year
    )
  • static void GieYearFromuser( int month, int year
    )
  • static void GetMonthFromUser( int month, int year
    )
  • static void Main( string args )

14
Example Implementing GetMonthName
  • static string GetMonthName(int month)
  • switch (month)
  • case 1 return ("January")
  • case 2 return ("February")
  • case 3 return ("March")
  • case 4 return ("April")
  • case 5 return ("May")
  • case 6 return ("June")
  • case 7 return ("July")
  • case 8 return ("August")
  • case 9 return ("September")
  • case 10 return ("October")
  • case 11 return ("November")
  • case 12 return ("December")
  • default return ("Illegal month")
  • // end of method GetMonthName

static void Main( string args )
Console.WriteLine( GetMonthName( 1 ) ) //
end of method Main
Test
15
Example Implementing IsLeapYear
  • static bool IsLeapYear(int year)
  • return ( (year 4 0) (year 100 ! 0)
    )
  • ( year 400 0)
  • // end of method IsLeapYear

static void Main( string args )
Console.WriteLine( IsLeapYear( 2000 ) )
Console.WriteLine( IsLeapYear( 1900 ) )
Console.WriteLine( IsLeapYear( 2003 ) ) // end
of method Main
16
Example Implementing GetMonthDays
  • static int GetMonthDays(int month, int year)
  • switch ( month )
  • case 2
  • if ( IsLeapYear( year ) )
  • return ( 29 )
  • else
  • return ( 28 )
  • case 4 case 6 case 9 case 11
  • return ( 30 )
  • default
  • return ( 31 )
  • // end of switch
  • // end of method GetMonthDays

static void Main( string args )
Console.WriteLine( GetMonthDays( 2, 2000 ) )
Console.WriteLine( GetMonthDays( 2, 1900 ) )
Console.WriteLine( GetMonthDays( 6, 2003 )
) // end of method Main
Write a Comment
User Comments (0)
About PowerShow.com