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

# 6401's solution

## to Leap in the Objective-C Track

Published at Feb 03 2020 · 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``````

### Leap/Leap_test/Leap.m

``````//
//  Leap.m
//  Leap_test
//
//  Created by Ð¡ÐµÑ€Ð³ÐµÐ¹ ÐŸÑƒÐ¿Ñ‡Ð¸Ðº on 03/02/2020.
//

#import "Leap.h"

@implementation Leap

-(id)initWithCalendarYear:(NSNumber*) date{
self = [super init];
int year = date.intValue;
if(self){
_isLeapYear = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
}
return self;
}

@end``````

### Leap/Leap_test/Leap.h

``````//
//  Leap.h
//  Leap_test
//
//  Created by Ð¡ÐµÑ€Ð³ÐµÐ¹ ÐŸÑƒÐ¿Ñ‡Ð¸Ðº on 03/02/2020.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Leap : NSObject
@property BOOL isLeapYear;
-(id) initWithCalendarYear:(NSNumber*) date;
@end

NS_ASSUME_NONNULL_END``````

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?