add leap exercise (#20)

* add leap exercise

* update

* for testing
This commit is contained in:
Lars Hvam
2021-11-27 09:51:19 +01:00
committed by GitHub
parent 6e69643de8
commit b5f15de4eb
6 changed files with 170 additions and 0 deletions

View File

@@ -44,6 +44,14 @@
"practices": [], "practices": [],
"prerequisites": [], "prerequisites": [],
"difficulty": 1 "difficulty": 1
},
{
"slug": "leap",
"name": "Leap",
"uuid": "ed451561-a84a-4b44-a3c9-9a0d9378667c",
"practices": [],
"prerequisites": [],
"difficulty": 1
} }
] ]
}, },

View File

@@ -0,0 +1,24 @@
# Description
Given a year, report if it is a leap year.
The tricky thing here is that a leap year in the Gregorian calendar occurs:
```text
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.
## 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][video].
[video]: http://www.youtube.com/watch?v=xX96xng7sAE

View File

@@ -0,0 +1,13 @@
{
"blurb": "Given a year, report if it is a leap year.",
"authors": ["larshp"],
"contributors": [
],
"files": {
"solution": ["zcl_leap.clas.abap"],
"test": ["zcl_leap.clas.testclasses.abap"],
"example": [".meta/zcl_leap.clas.abap"]
},
"source": "JavaRanch Cattle Drive, exercise 3",
"source_url": "http://www.javaranch.com/leap.jsp"
}

View File

@@ -0,0 +1,22 @@
CLASS zcl_leap DEFINITION PUBLIC.
PUBLIC SECTION.
METHODS leap
IMPORTING
year TYPE i
RETURNING
VALUE(result) TYPE abap_bool.
ENDCLASS.
CLASS zcl_leap IMPLEMENTATION.
METHOD leap.
IF year MOD 4 = 0 AND year MOD 100 <> 0.
result = abap_true.
ELSEIF year MOD 400 = 0.
result = abap_true.
ELSE.
result = abap_false.
ENDIF.
ENDMETHOD.
ENDCLASS.

View File

@@ -0,0 +1,16 @@
CLASS zcl_leap DEFINITION PUBLIC.
PUBLIC SECTION.
METHODS leap
IMPORTING
year TYPE i
RETURNING
VALUE(result) TYPE abap_bool.
ENDCLASS.
CLASS zcl_leap IMPLEMENTATION.
METHOD leap.
* add solution here
ENDMETHOD.
ENDCLASS.

View File

@@ -0,0 +1,87 @@
CLASS ltcl_leap DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
PRIVATE SECTION.
DATA cut TYPE REF TO zcl_leap.
METHODS setup.
METHODS test_2015 FOR TESTING.
METHODS test_1970 FOR TESTING.
METHODS test_1996 FOR TESTING.
METHODS test_1960 FOR TESTING.
METHODS test_2100 FOR TESTING.
METHODS test_1900 FOR TESTING.
METHODS test_2000 FOR TESTING.
METHODS test_2400 FOR TESTING.
METHODS test_1800 FOR TESTING.
ENDCLASS.
CLASS ltcl_leap IMPLEMENTATION.
METHOD setup.
cut = NEW zcl_leap( ).
ENDMETHOD.
METHOD test_2015.
* year not divisible by 4 in common year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 2015 )
exp = abap_false ).
ENDMETHOD.
METHOD test_1970.
* year divisible by 2, not divisible by 4 in common year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 1970 )
exp = abap_false ).
ENDMETHOD.
METHOD test_1996.
* year divisible by 4, not divisible by 100 in leap year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 1996 )
exp = abap_true ).
ENDMETHOD.
METHOD test_1960.
* year divisible by 4 and 5 is still a leap year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 1960 )
exp = abap_true ).
ENDMETHOD.
METHOD test_2100.
* year divisible by 100, not divisible by 400 in common year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 2100 )
exp = abap_false ).
ENDMETHOD.
METHOD test_1900.
* year divisible by 100 but not by 3 is still not a leap year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 1900 )
exp = abap_false ).
ENDMETHOD.
METHOD test_2000.
* year divisible by 400 is leap year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 2000 )
exp = abap_true ).
ENDMETHOD.
METHOD test_2400.
* year divisible by 400 but not by 125 is still a leap year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 2400 )
exp = abap_true ).
ENDMETHOD.
METHOD test_1800.
* year divisible by 200, not divisible by 400 in common year
cl_abap_unit_assert=>assert_equals(
act = cut->leap( 1800 )
exp = abap_false ).
ENDMETHOD.
ENDCLASS.