# project involving leap years and days in the month

Page 1 of 1

## 4 Replies - 445 Views - Last Post: 16 April 2012 - 02:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=275419&amp;s=1ac4ea062beef47a879598a9cce7f4a8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 slazy

Reputation: 0
• 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”);

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):”);
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 {
}
catch {
return defaultVal;
}
}
}

```

Complete the if statement that uses the value returned by IsLeap to output appropriate messages for the user as follows:

1984 is a leap year

Another session may look as follows:

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:

The number of days is 31

Another session may look as follows:

The number of days is 30

Another session may look as follows:

The number of days is 30

Another session may look as follows:

“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:

The number of days is 31

Another session may look as follows:

The number of days is 28

Another session may look as follows:

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

Is This A Good Question/Topic? 0

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

### #2 DimitriV

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

Reputation: 544
• 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

### #3 slazy

Reputation: 0
• 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

### #4 Andrei95

Reputation: 1
• 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.

### #5 Ionut

• D.I.C Lover

Reputation: 385
• 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>;

```