A.7. Chapter 9
A.7.1. Exercise 1 Solution
The definitions would be as follows:
//============ static members =============== private static string[] daysOfWeek = new string[] {"", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"}; //============ instance members =============== private string lastName; private string zipCode;
You would want the daysOfWeek array to be static since all instances of the class could then share this data. Also, we added an empty element at the front of the array because I tend to think of Monday as the first day of the week.
A.7.2. Exercise 2 Solution
One solution might be written like this:
/***** * Purpose: Return the number of days in a given month. * * Parameter list: * int month the month " " * int year the year under consideration * * Return value: * int the number of days in the month or 0 on error *****/ public int getDaysInMonth(int month, int year) { int days; if (month < 1 || month > 12 || year < 1 || year > 9999) { return 0; } if (month != 2) // As long as it's not February { days = daysInMonth[month]; } else { days = daysInMonth[2] + getLeapYear(year); } return days; }
A discussion of this code appears in Chapter 10.
A.7.3. Exercise 3 Solution
I couldn't think of one, either.
A.7.4. Exercise 4 Solution
First, you would need to define a temporary string:
string buff;
Now replace the statement with this:
buff = year.ToString() + " is ";
if (leap == 1) // leap is 1 for a leap year { buff += "a leap year"; } ...
Get Beginning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.