Decomposition And Calendar Program, Lecture Slide - Computer Science, Slides of C programming

Variables scope and duration, Example, Step wise refinement, Calender requirements, Starting at the top, The current todo list, Implementing giveinstructions, Geryearfromuser, Getmonthfromuser, Printyear, The todo list revisited, Printmonth, Design Printmonth, Representing Weekdays, Getmonthname, Isleapyear, Getmonthdays, Getfristweekdays indentinitialweekdays printdays

Typology: Slides

2010/2011

Uploaded on 10/05/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #15:
Decomposition and
the Calendar Program
http://flint.cs.yale.edu/cs112/
2
Outline
rAdmin. and review
rStepwise refinement using the Calendar
program as an example
3
Recap: Variable Scope and Duration
rScope
mA local variable is accessible after it is declared
and before the end of the block
mA class variable is accessible in the whole class
mParameter passing with ref and out makes
some variables aliases of others
rDuration
mA 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.
4
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 < 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 > 0) return temp;
}
// now only x and NoOfTries in scope
return 0;
} // AskForPositiveNumber
static void Main( string[] args )
{…}
}
5
Stepwise Refinement
rmain idea: use methods to divide a large
programming problem into smaller pieces that are
individually easy to understand ------- decomposition
rstepwise refinement (or top-down design)
mstart with the main program
mthink about the problem as a whole and identify the major
pieces of the entire task
mwork each of these big pieces one by one
mfor each piece, think what is its major sub-pieces, and repeat
this process
rrun the calendar example
6
Calendar: Requirements
rGet a year from the user, the earliest year should be
1900
rGet a month from the user, the input should be from
0 to 12
mIf 0, print calendar for all 12 months
mOtherwise, print the calendar of the month of the year
rEach month is displayed as: 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
pf3
pf4
pf5

Partial preview of the text

Download Decomposition And Calendar Program, Lecture Slide - Computer Science and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #15:

Decomposition and

the Calendar Program

http://flint.cs.yale.edu/cs112/

Outline

r Admin. and review

r Stepwise refinement using the Calendar

program as an example

Recap: Variable Scope and Duration

r Scope

m A local variable is accessible after it is declared

and before the end of the block

m A class variable is accessible in the whole class

m Parameter passing with ref and out makes

some variables aliases of others

r Duration

m 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.

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 < 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 > 0) return temp; } // now only x and NoOfTries in scope return 0; } // AskForPositiveNumber static void Main( string[] args ) {…} }

Stepwise Refinement

r main idea: use methods to divide a large

programming problem into smaller pieces that are

individually easy to understand ------- decomposition

r stepwise refinement (or top-down design)

m start with the main program

m think about the problem as a whole and identify the major

pieces of the entire task

m work each of these big pieces one by one

m for each piece, think what is its major sub-pieces, and repeat

this process

r run the calendar example

Calendar: Requirements

r Get a year from the user, the earliest year should be

r Get a month from the user, the input should be from

0 to 12

m If 0, print calendar for all 12 months

m Otherwise, print the calendar of the month of the year

r Each month is displayed as:

January 1900

Sun Mon Tue Wed Thu Fri Sat

Design

year = GetYearFromUser()

month = GetMonthFromUser()

month ?= 0

PrintMonth(month, year) PrintYear(year)

f

t

Calendar: Starting at the Top

//========================================================= // File: Calendar.cs CS112 Demo // Author: CS112 Instructor Email: [email protected] // Class: Calendar // -------------------- // This program prints a calendar of a year or a month of a year. //==========================================================

using System;

class Calendar { static void Main ( string[] args ) { int year, month;

GiveInstructions(); year = GetYearFromUser(); month = GetMonthFromUser(); if ( month == 0 ) PrintYear( year ); else PrintMonth( month, year );

} // end of method Main }

Calendar: the Current Todo List

// Method prototypes (i.e., Method headers):

// static void GiveInstructions();

// static int GetYearFromUser();

// static int GetMonthFromUser();

// static void PrintYear( int year );

// static void PrintMonth( int month, int year );

Implementing GiveInstructions

// GiveInstructions prints out the instructions to the user

static void GiveInstructions()

Console.WriteLine("This program displays a calendar for a full");

Console.WriteLine("year. The year must not be before 1900.");

} // end of method GiveInstructions

Implementing GetYearFromUser

// GetYearFromUser reads in a year from the user and returns

// that value. If the user enters a year before 1900, the

// function gives the user another chance.

static int GetYearFromUser()

int year = 1900;

do

Console.Write("Which year [1900-]? ");

year = Int32.Parse( Console.ReadLine( ) );

} while (year < 1900);

return year;

} // end of method GetYearFromUser

Implementing GetMonthFromUser

// GetMonthFromUser reads in a month from the user and returns

// that value. The input must be between 0 and 12.

static int GetMonthFromUser()

int month = 0;

do

Console.Write("Which month [0-12; 0 for all months]? ");

month = Int32.Parse( Console.ReadLine( ) );

} while ( month < 0 || month > 12 );

return month;

} // end of method GetMonthFromUser

Implementing PrintMonth(month, year)

static void PrintMonth( int month, int year )

Console.WriteLine( " " + GetMonthName( month ) + " " + year );

Console.WriteLine( " Sun Mon Tue Wed Thu Fri Sat" );

// get the weekday of the first day

int weekday = GetFirstWeekday( month, year );

// indent the initial weekdays

IndentInitialWeekdays( weekday );

// get number of days of the month

int nDays = GetMonthDays( month, year );

// print all of the days

printDays(weekday, nDays);

} // end of method PrintMonth

The Current Todo List

static string GetMonthName( int month );

// The English word for a specific month

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 bool IsLeapYear( int month, int year );

// Check if a year is a leap year

static void PrintDays( int weekday, int nDays );

// Printing each day of the month, wrap around properly

static void PrintMonth( int month, int year )

static void PrintYear( int month, int year );

static void GiveInstructions( int month, int year );

static void GetYearFromuser( int month, int year );

static void GetMonthFromUser( int month, int year );

static void Main( string[] args );

Implementing “Library” Methods

r Important

m Implement simple methods first

m Test a method

• using some dummy method calls, or call the method in

Main() with some simple test cases

• using the Visual Studio Immediate window or the

Debugger

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 ) );

Test: } // end of method Main

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

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

Implementing GetFirstWeekday

static int GetFirstWeekday(int month, int year) { int i;

int weekday = Monday; // 1900.1.1 is a Monday

for ( i = 1900; i < year; i++ ) { weekday = (weekday + 365) % 7; if ( IsLeapYear(i) ) weekday = (weekday + 1) % 7; } for ( i = 1; i < month; i++ ) { weekday = (weekday + GetMonthDays( i, year )) % 7; } return (weekday);

} // method GetFirstWeekday

static void Main( string[] args ) { Console.WriteLine( GetFirstWeekday( 2, 2000 ) ); Console.WriteLine( GetFirstWeekday( 2, 1900 ) ); Console.WriteLine( GetFirstWeekday( 6, 2003 ) ); } // end of method Main

26

Implementing IndentInitialWeekdays

// IndentInitialWeekdays(weekday) indents the first line of

// the calendar by printing enough blank spaces to get to the

// position on the line corresponding to weekday.

static void IndentInitialWeekdays( int weekday )

for ( int i = 0; i < weekday; i++ )

Console.Write(" ");

} // end of method IndentInitialWeekdays

Implementing PrintDays

// PrintDays (weekday, nDays) prints the actual days assuming // that the first day of this month falls on the "weekday" // and there are "nDays" days for the month. //=========================================================== static void printDays( int weekday, int nDays ) { // print all of the days for ( int day = 1; day <= nDays; day++ ) { if ( day > 9 ) Console.Write(" " + day); else Console.Write(" " + day); // deal with 01 vs 12

if ( weekday == Saturday ) Console.WriteLine();

weekday = (weekday + 1) % 7; } // end of for loop

if ( weekday != Sunday ) Console.WriteLine();

} // end of method PrintDays

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