July 2023
Intermediate to advanced
276 pages
6h 55m
English
Traditional for loops allow us to increment or decrement the range by different step values. Let’s take a look at a countFrom1900 method of a LeapYears class that computes the number of leap years since 1900 to a given year.
Let’s start with the test first:
| | public class LeapYearsTest { |
| | LeapYears leapYears; |
| | |
| | @BeforeEach |
| | public void init() { |
| | leapYears = new LeapYears(); |
| | } |
| | |
| | @Test |
| | public void countFrom1900() { |
| | assertAll( |
| | () -> assertEquals(25, leapYears.countFrom1900(2000)), |
| | () -> assertEquals(27, leapYears.countFrom1900(2010)), |
| | () -> assertEquals(31, leapYears.countFrom1900(2025)), |
| | () -> assertEquals(49, leapYears.countFrom1900(2100)), ... |
Read now
Unlock full access