4 Replies - 445 Views - Last Post: 16 April 2012 - 02:38 AM Rate Topic: -----

#1 slazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 12

project involving leap years and days in the month

Posted 15 April 2012 - 11:53 PM

Got this form given to me in class and I have no idea where to begin with it,
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!!!

:code:

This post has been edited by JackOfAllTrades: 16 April 2012 - 12:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: project involving leap years and days in the month

#2 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: project involving leap years and days in the month

Posted 16 April 2012 - 12:17 AM

Leap years are every 4 years or if the year is a century then it is only a leap year when it is divisible by 400.
Ie:
if (year % 100 = 0) {
if (year % 400 = 0) {
cout << "Leap year";
return true;
}
else
{
cout << "Nicht a leap year";
return false;
}
}
else
{
if (year % 4 = 0)
{
cout << "Leap year";
return true;
}
else
{
cout << "Nicht a leap year";
return false;
}
}


I've probably got my if statements wrong but do you get the idea?

This post has been edited by DimitriV: 16 April 2012 - 12:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 slazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 12

Re: project involving leap years and days in the month

Posted 16 April 2012 - 12:19 AM

kind of, my main problem is with the if statements, and getting them to correctly read the year thats ewntered through safereadinteger,
at the moment my program displays messages but its no dependant on the input so even if i put 2001 it says its a leap year
ill try this out, thanks for your help :)
Was This Post Helpful? 0
  • +
  • -

#4 Andrei95  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 17-December 11

Re: project involving leap years and days in the month

Posted 16 April 2012 - 12:59 AM

Hi! You should take a look here:

http://5dspace-time..../Algorithm.html

I guess that this will respond to some of your questions. :turned:
Was This Post Helpful? 0
  • +
  • -

#5 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: project involving leap years and days in the month

Posted 16 April 2012 - 02:38 AM

Quote

moment my program displays messages but its no dependant on the input so even if i put 2001 it says its a leap year

This is because you put Console.WriteLine (“a leap year”); outside the if statement, so that line gets executed everytime. You should use else
if (statement that returns a boolean value)
{
   //code here if statement true
} 
else 
{
    //code here if statement false
}
//the rest of the code that is executed regardless "if" statement.


Also, for future development, if you have single assignments to do inside a if..then..else, use ?: operator
value = (StatementThatReturnsABooleanValue) ? <valueIfTrue>:<valueIfFalse>;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1