if someone can point me in the right direction id greatly appreciate it
ISIT111 Lab 4
In this lab, you will practice the following:
- Writing your own methods and calling them.
- Making decisions (using if statements) when to call the methods.
- Using if statements to make decisions within the methods.
- Making decisions (use if statements) based on return values of the methods.
Note all your answers need to be in the same C# file; and you must use the code shown below, your tasks involve adding to it. The first step is to understand the code and the structure of the program given.
Exercise 1 (1 mark)
A year is a leap year if it is divisible 4 but not by 100 (e.g. 1900 was not leap year, 1984 was a leap year). If it divisible by 100 then it is not a leap year unless it is divisible by 400 (1900 was not a leap year but year 2000 was a leap year. Year 2400 will also be a leap year but year 2100 will not be a leap year). The method IsLeap takes an integer parameter (a year) and it returns a Boolean value. If the year is a leap year, it returns true otherwise it returns false.
The header for IsLeap in the incomplete program code is shown below for your convenience:
class Program
{
static void Main ( )
{
int year = 0;
bool leap = false;
year = GetYear();
Console.WriteLine (“{0} is”, year);
if (! IsLeap (year))
{
// enter rest of your code here for Exercise 1
}
Console.WriteLine (“a leap year”);
Console.WriteLine (“Please enter a month:”);
string month = Console.ReadLine ();
int daysinmonth = ConvertDate(month);
// Enter your code here for Exercises 2 and 3
}
// The following five methods are needed in this lab.
static bool IsLeap(int year)
{
bool leap = false;
leap = ( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0;
return leap;
}
static int GetYear()
{
int year = 0;
Console.WriteLine(“Please enter a year (a whole number larger or equal to 0):”);
year = SafeReadInteger (0);
return year;
}
static int ConvertDate(string month)
{
if (month == "January" || month == "january") return (31);
// enter the rest of your code here for Exercise 2
}
static void CountUpTo(int limit)
{
int counter = 0;
while (counter < limit)
{
Console.WriteLine(counter);
counter++;
}
}
public static int SafeReadInteger (int defaultVal)
{
try {
return int.Parse (System.Console.ReadLine ());
}
catch {
return defaultVal;
}
}
}
Complete the if statement that uses the value returned by IsLeap to output appropriate messages for the user as follows:
Please enter a year: ¬1984
1984 is a leap year
Another session may look as follows:
Please enter a year: ¬2007
2007 is not a leap year
Exercise 2 (1 mark):
Write a function method ConvertDate which has a string (a month) parameter and returns an integer value indicating the number of days in the month in the parameter. If an illegal name parameter is passed to the function, it returns 0. Prompt the user to enter a month, call ConvertDate and display its output. Month names are treated as proper nouns so the first letter needs to be capitalised. However, assume that some users know this and others do not. In other words, ConvertDate needs to be robust enough to accept any valid month name even if it is not capitalised (For other than the first letter, you can assume that they are always lower case). Note the output of ConvertDate can be one of the following: 0, 28, 30 or 31. It is 0 if the month name is unkonwn, 28 if it is February, 30 or 31 (depending on the month).
static int ConvertDate(string month)
{
if (month == "January" || month == "january") return (31);
// complete the method for the remaining 11 months and the error case
}
In addition, write additional statements within your main so that the console session may look as follows:
Please enter a month: march
The number of days is 31
Another session may look as follows:
Please enter a month: April
The number of days is 30
Another session may look as follows:
Please enter a month: april
The number of days is 30
Another session may look as follows:
Please enter a month: alll
“alll” is an unknown month
Exercise 3 (1 mark):
In this exercise, you will take into account that February can be 29 days if it is during a leap year. Do NOT change the implementation of ConvertDate. Instead, add additional code in your main so that before you display the output of the method, if the month entered by the user is February, prompt the user for the year, and then use IsLeap to check the year and adjust the output of ConvertDate accordingly.
Write new and appropriate method calls and statements within your main so that the console session may look as follows:
A valid console session may look as follows:
Please enter a month: March
The number of days is 31
Another session may look as follows:
Please enter a month: february
Please enter a year: 2007
The number of days is 28
Another session may look as follows:
Please enter a month: february
Please enter a year: 2000
The number of days is 29
Exercise 4 (1 mark):
static void CountUpTo(int limit)
{
int counter = 0;
while (counter < limit)
{
Console.WriteLine(counter);
counter++;
}
}
Change the above method to use a for loop instead of the while loop. Add appropriate statements within main to call on the method (that is, ensure you have an input statement to take a value for limit and to call on the method CountUpTo.
You may loose up to one mark if your code is not easy to read. Therefore take care to uniformly indent, to use const where needed and to appropriately name your variables (i.e. not just x, y, z …). You will be asked questions about the code your present to the assessor. To get full marks for any of the above questions, you will to be able to answer questions about the code your present to the assessor.
MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!
This post has been edited by JackOfAllTrades: 16 April 2012 - 12:00 PM

New Topic/Question
Reply



MultiQuote



|