ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# xhruso00's solution

## to Leap in the Objective-C Track

Published at May 02 2019 · 0 comments
Instructions
Test suite
Solution

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

``````on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
``````

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.

## Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

## Setup

There are two different methods of getting set up to run the tests with Objective-C:

• Create an Xcode project with a test target which will run the tests.
• Use the ruby gem `objc` as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

### Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

``````exercism submit <path-to-exercism-workspace>/objective-c/leap/Leap.m
``````

You can find the Exercism workspace by running `exercism debug` and looking for the line beginning with Workspace.

## Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### LeapTest.m

``````#import <XCTest/XCTest.h>

#if __has_include("LeapExample.h")
# import "LeapExample.h"
# else
# import "Leap.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface LeapTest : XCTestCase

@end

@implementation LeapTest

- (void)testVanillaLeapYear {
Leap *year = [[Leap alloc] initWithCalendarYear:@1996];
XCTAssert(year.isLeapYear);
}

- (void)testAnyOldYear {
Leap *year = [[Leap alloc] initWithCalendarYear:@1997];
XCTAssertFalse(year.isLeapYear);
}

- (void)testCentury {
Leap *year = [[Leap alloc] initWithCalendarYear:@1900];
XCTAssertFalse(year.isLeapYear);
}

- (void)testExceptionalCentury {
Leap *year = [[Leap alloc] initWithCalendarYear:@2400];
XCTAssert(year.isLeapYear);
}

@end
NS_ASSUME_NONNULL_END``````
``````#import "Leap.h"
#import "time.h"

@implementation Leap

- (instancetype)initWithCalendarYear:(NSNumber *)aYear
{
self = [super init];
if (self) {
_year = aYear;
}
return self;
}

- (BOOL)isLeapYear
{
NSInteger year = [[self year] integerValue];
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

@end``````