Challenge: FizzBuzz
Develop an application for a simple game, FizzBuzz, using TDD with Java.
We'll cover the following
Task and requirements
Your task is to implement the FizzBuzz game using the test-driven development (TDD) approach. FizzBuzz is a simple game where you count numbers, but replace multiples of 3 with "Fizz"
and multiples of 5 with "Buzz"
. If a number is a multiple of both 3 and 5, you say "FizzBuzz"
.
Sample Input: [1, 2, 3, 4, 5, 6, 7, 10, 15, 30]
Sample Output: "1, 2, Fizz, 4, Buzz, Fizz, 7, Buzz, FizzBuzz, FizzBuzz"
Follow the TDD cycle by writing test cases first and then implementing the function. Write multiple test cases to cover different scenarios, including various combinations of numbers.
Create a FizzBuzz
class with the following methods:
processInput(int[] numbers)
: Takes an array of integers as input and returns a string with numbers replaced according to the FizzBuzz rules.processNumber(int number)
: Takes an integer as input and returns the FizzBuzz output as a string. Implement the FizzBuzz game logic as follows:If the number is a multiple of 3, return
"Fizz"
.If the number is a multiple of 5, return
"Buzz"
.If the number is a multiple of both 3 and 5, return
"FizzBuzz"
.If none of the above conditions are met, return the number as a string.
Try it yourself
Add or modify the code in the widget below to implement your solution.
Get hands-on with 1200+ tech skills courses.