TDD로 숫자 뒤집기 - 2부
요구 사항을 코드로 옮겼으니, 이제 실제 구현을 작성해 볼 차례예요! 이전 연습 문제의 후속 과제로, 방금 작성한 단위 테스트를 포함해 대부분의 코드가 동일하게 제공됩니다. 이번 과제에서는
모듈로 10(% 10)을 사용해 reverse() 메서드를 구현하세요.
이 연습은 강의의 일부입니다
Java 테스트 소개
연습 안내
- 각 반복에서
% 10을 사용해 원래 숫자의 마지막 자릿수를 구하세요. - 각 반복에서
/ 10을 사용해 원래 숫자의 마지막 자릿수를 버리세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
public class IntReverser {
public static int reverse(int num) {
int inverted = 0;
while (num != 0) {
// At every iteration take the last digit with % 10 and add to inverted * 10.
inverted = inverted * 10 + ____;
// Discard the last digit using / 10.
num = ____;
}
return inverted;
}
public static class IntReverserTest {
@Test
public void testReverse_reversesNumber() {
int input = 1234;
int expected = 4321;
int actual = IntReverser.reverse(input);
assertEquals(expected, actual);
}
}
public static void main(String[] args) {
launchTestsAndPrint(IntReverserTest.class);
}
}