CodingBat: Java. Update 2013.2


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


The Array-2 section on Coding Bat saw an update as well. Those four exercises gradually build up to a variation of the well-known FizzBuzz problem.

All solutions were successfully tested on 29 March 2013.

fizzArray:

public int[] fizzArray(int n) {
	int[] result = new int[n];
	for (int i = 0; i < n; i++)
		result[i] = i;
	return result;
}

fizzArray2:

public String[] fizzArray2(int n) {
	String[] result = new String[n];
	for (int i = 0; i < n; i++)
		result[i] = String.valueOf(i);
	return result;
}

fizzArray3:

public int[] fizzArray3(int start, int end) {
	int n = end - start;
	int[] result = new int[n];

	for (int i = 0; i < n; i++)
		result[i] = start++;
	return result;
}

fizzBuzz:

public String[] fizzBuzz(int start, int end) {
	int n = end - start;
	String[] result = new String[n];

	int pos = 0;
	for (int i = start; i < end; i++) {
		boolean fizz = i % 3 == 0;
		boolean buzz = i % 5 == 0;

		if (fizz && buzz) result[pos] = "FizzBuzz";
		else if (fizz) result[pos] = "Fizz";
		else if (buzz) result[pos] = "Buzz";
		else result[pos] = String.valueOf(i);
		pos++;
	}
	return result;
}

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


6 thoughts on “CodingBat: Java. Update 2013.2

  1. Dror Bar-Gil

    Even though it pass all CodingBat tests, your code for fizzBuzz in not completely correct.
    It will fail when start=end
    For example:
    Running fizzBuzz(11, 11) will fail.

    Because you try to initialize an array of size 0;

    Dror

    Reply
    1. Gregor Ulm Post author

      The problem is that your conception of what the program should do is different from what it is supposed to do. Please have a look at the specification of the FizzBuzz problem on the CodingBat website. There you will read:
      “Consider the series of numbers beginning at start and running up to but not including end.” If you set start equal to end, then the program (correctly) initializes an array of length zero. Consequently, it will also return an array of length zero, which is, again, correct. Thus, FizzBuzz does not “fail” but merely behaves according to the specification.

      Reply
  2. ShomyLee

    Hi Gregor,

    Could you tell me why did you use else if statement and not if on line 11 and 12?
    What’s he difference between if and else if in this program?

    Reply
    1. Gregor Ulm Post author

      The else if statement ensures that you execute only one of the conditions. By using a number of if statements you would execute each if that meets the given condition, even when this is not desired. Remember, you are supposed to print “FizzBuzz” if both conditions (“fizz” and “buzz”) are met. Of course, if you meet the conditions for both “fizz” and “buzz”, then you also meet the conditions for both “fizz” and “buzz” alone. This would therefore lead to erroneous output.

      Reply
  3. Rickyg

    For the FizzBuzz, my approach was similar to yours. But I used the index variable (“pos” in your case) inside the for loop.
    for (int i = start, pos = 0; i < end; i++, pos++)

    Just a simple question, does it make much difference (for example in computation time) in doing so?

    Reply
    1. Gregor Ulm Post author

      It is completely irrelevant for performance how you name things as variable names are translated to labels in JVM anyway.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.