I have been given the classic FizzBuzz test to do. However, the problem has an extra task which is to display the results in an ArrayList. I know that you can't just stick an integer and string values together in a list. Hence, this is causing problems for me.
I have made an attempt on converting the numbers gotten from the loop to string however, it became too complicated and I thought maybe I was over-complicating things. Or maybe I am doing something wrong for an easy task such as this to be so time-consuming.
Below I have included the most recent code I have tried and the guide comments I was given.
- Implement the loop that will take the List Range and parse for the values to solve FizzBuzz.
- You will need to add a successfully value within the loop to the Private Class variable 'private List range'.
- As an example, while looping 3 will equal fizz which should be added to the fizzBuzzList.
The two private variables have been set below for you to hold both the list and the FizzBuzz List.
public class FizzBuzz { private List range; private List fizzBuzzList = new ArrayList(); public FizzBuzz(int startOfRange, int endOfRange){ range = IntStream.rangeClosed(startOfRange, endOfRange).boxed().collect(Collectors.toList()); fizzBuzzIterator(range); } public void fizzBuzzIterator(List range){ for (int i = 1 ; i < range.size(); i++) { if (((i % 3) == 0) && ((i % 5) == 0)) { fizzBuzzList.add("FizzBuzz"); } else if ((i % 3) == 0) { fizzBuzzList.add("Fizz"); } else if ((i % 5) == 0) { fizzBuzzList.add("Buzz"); } else { fizzBuzzList.add(i); } } } public List getFizzBuzzList() { return fizzBuzzList; } }
Below is the test code which I am supposed to test the results on.
public class FizzbuzzTests {
FizzBuzz fizzBuzz = new FizzBuzz(1,16);
@Test
public void fizzBuzzIteratorTest(){
List<String> minFizzBuzzList = List.of("1","2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz");
Assert.assertEquals(fizzBuzz.getFizzBuzzList(), minFizzBuzzList);
}
}
The expected results as I am shown is supposed to be in the format such as [1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]. The code written above gives me the results in the same format, however, the difference is that one utilizes "java.util.ArrayList" while other utilizes "java.util.ImmutableCollections$ListN" and thus this gives me an error.
I have been stuck trying to fix this for a while now and I am running out of ideas to try. Any advice would be much helpful and appreciated. Thanks.