Skip to content

Commit 3511b08

Browse files
committed
Updated FizzBuzz - Koan and TDD
1 parent 77c7fa8 commit 3511b08

5 files changed

Lines changed: 117 additions & 1 deletion

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.teachingkidsprogramming.recipes.completed;
2+
3+
public class FizzBuzz
4+
{
5+
public static void main(String[] args)
6+
{
7+
StringBuilder sb = new StringBuilder();
8+
//for the whole numbers from 1 to 100, print either that number, or,
9+
for (int i = 1; i <= 100; i++)
10+
{
11+
sb.append(convert(i));
12+
}
13+
System.out.print(sb);
14+
}
15+
public static String convert(int i)
16+
{
17+
//if that number is evenly divisible by either 3 or 5, then print the word 'FizzBuzz'
18+
if (0 == i % 15) { return "\n FizzBuzz"; }
19+
//if that number is evenly divisible by 5, then print the word 'Buzz',
20+
if (0 == i % 5) { return "\n Buzz"; }
21+
//if that number is evenly divisible by 3, then print the word 'Fizz',
22+
if (0 == i % 3) { return "\n Fizz"; }
23+
return "\n " + i;
24+
}
25+
//for more complete directions see this page
26+
//https://www.penflip.com/lynnlangit/tkp-lesson-plans/blob/master/course09.txt
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.teachingkidsprogramming.recipes.completed;
2+
3+
public class FizzBuzzTDD
4+
{
5+
public static String convert(int i)
6+
{
7+
if (0 == i % 15) { return "FizzBuzz"; }
8+
if (0 == i % 5) { return "Buzz"; }
9+
if (0 == i % 3) { return "Fizz"; }
10+
return "" + i;
11+
}
12+
//for the numbers being tested, print out either that number, or,
13+
//if that number is evenly divisible by 3, then print the word 'Fizz',
14+
//if that number is evenly divisible by 5, then print the word 'Buzz',
15+
//if that number is evenly divisible by either 3 or 5, then print the word 'FizzBuzz'
16+
//
17+
//write tests using the Assert object via the TDD style
18+
//
19+
//for more complete directions see this page
20+
//https://www.penflip.com/lynnlangit/tkp-lesson-plans/blob/master/course09.txt
21+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.teachingkidsprogramming.recipes.completed;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class FizzBuzzTDDTest
8+
{
9+
@Test
10+
public void test1Returns1()
11+
{
12+
String result = FizzBuzzTDD.convert(1);
13+
assertEquals("1", result);
14+
}
15+
@Test
16+
public void test2Returns2()
17+
{
18+
String result = FizzBuzzTDD.convert(2);
19+
assertEquals("2", result);
20+
}
21+
@Test
22+
public void test3ReturnsFizz()
23+
{
24+
String result = FizzBuzzTDD.convert(3);
25+
assertEquals("Fizz", result);
26+
}
27+
@Test
28+
public void test5ReturnsBuzz()
29+
{
30+
String result = FizzBuzzTDD.convert(5);
31+
assertEquals("Buzz", result);
32+
}
33+
@Test
34+
public void test6ReturnsFizz()
35+
{
36+
String result = FizzBuzzTDD.convert(6);
37+
assertEquals("Fizz", result);
38+
}
39+
@Test
40+
public void test10ReturnsBuzz()
41+
{
42+
String result = FizzBuzzTDD.convert(10);
43+
assertEquals("Buzz", result);
44+
}
45+
@Test
46+
public void test15ReturnsFizzBuzz()
47+
{
48+
String result = FizzBuzzTDD.convert(15);
49+
assertEquals("FizzBuzz", result);
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.teachingkidsprogramming.section08tdd;
2+
3+
public class FizzBuzz
4+
{
5+
//for the whole numbers from 1 to 100, print either that number, or,
6+
//if that number is evenly divisible by 3, then print the word 'Fizz',
7+
//if that number is evenly divisible by 5, then print the word 'Buzz',
8+
//if that number is evenly divisible by either 3 or 5, then print the word 'FizzBuzz'
9+
//
10+
//NOTE: this is a kata (higher level instructions)
11+
//part of the exercise is to translate into line-by-line English, THEN Java
12+
//
13+
//for more complete directions see this page
14+
//https://www.penflip.com/lynnlangit/tkp-lesson-plans/blob/master/course09.txt
15+
}

src/org/teachingkidsprogramming/section08tdd/FizzBuzzTDD.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
public class FizzBuzzTDD
44
{
5-
//for the whole numbers from 1 to 100, print either that number, or,
5+
//for the numbers being tested, print out either that number, or,
66
//if that number is evenly divisible by 3, then print the word 'Fizz',
77
//if that number is evenly divisible by 5, then print the word 'Buzz',
88
//if that number is evenly divisible by either 3 or 5, then print the word 'FizzBuzz'
9+
//
910
//write tests using the Assert object via the TDD style
11+
//
1012
//for more complete directions see this page
1113
//https://www.penflip.com/lynnlangit/tkp-lesson-plans/blob/master/course09.txt
1214
}

0 commit comments

Comments
 (0)