



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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 ) {…} }
//========================================================= // 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 }
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 ) );
static void Main( string[] args ) { Console.WriteLine( IsLeapYear( 2000 ) ); Console.WriteLine( IsLeapYear( 1900 ) ); Console.WriteLine( IsLeapYear( 2003 ) ); } // end of method Main
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
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