I am working on a fizzbuzz game programming project. In this project, I will com
ID: 3743874 • Letter: I
Question
I am working on a fizzbuzz game programming project.
In this project, I will complete the code in FizzBuzz.java to implement the FizzBuzz game. FizzBuzz is a counting game with many variations. If you’ve seen this game before, please read carefully because our game below is different.
To begin, the players agree on a fizz number and a buzz number, both single-digit integers between 1 and 9. The players then take turns saying the fizzbuzz value for each successive integer starting from 1. The rules are:
• If the integer is divisible by the fizz number, or if it contains the digit fizz number, you must say ‘fizz’;
• If the integer is divisible by the buzz number, or if it contains the digit buzz number, you must say ‘buzz’;
• However, if both the above (i.e. fizz and buzz) conditions are true, you must say ‘fizzbuzz’;
• If none of the above conditions is true, say the integer itself.
For example, if the fizz number is 3, and the buzz number is 4, then the first twenty fizzbuzz values are: 1, 2, fizz, buzz, 5, fizz, 7, buzz, fizz, 10, 11, fizzbuzz, fizz, buzz, fizz, buzz, 17, fizz, 19, buzz Note that the fizzbuzz value of 13 is ‘fizz’, because it contains the digit 3 (even though 13 is not divisible by 3). Similarly: the fizzbuzz value of 31 is ‘fizz’; however, that of 32 is ‘fizzbuzz’ because 32 both contains the digit 3 and is divisible by 4, thereby meeting both the fizz and buzz conditions.
Here's the FizzBuzz.java:
package fizzbuzz;
public class FizzBuzz {
private final int fizzNumber;
private final int buzzNumber;
/**
* Construct an object that can compute fizzbuzz values for a game of
* Fizz and Buzz.
* @param fizzNumber: an integer between 1 and 9
* @param buzzNumber: an integer between 1 and 9
*/
public FizzBuzz(int fizzNumber, int buzzNumber) {
this.fizzNumber = fizzNumber;
this.buzzNumber = buzzNumber;
}
/**
* Returns the fizzbuzz value for n. The rules are:
* - if n is divisible by fizzNumber, or contains the digit fizzNumber, return "fizz"
* - if n is divisible by buzzNumber, or contains the digit buzzNumber, return "buzz"
* - however, if both the above conditions are true, you must return "fizzbuzz"
* - if none of the above conditions is true, return the number itself.
* For example, getValue(1) returns "1".
* @param n: a positive integer
* @return the fizzbuzz value, as a String
*/
public String getValue(int n) {
if(n==3) return "fizz";
if(n==4) return "buzz";
return Integer.toString(n); // return the number itself as a String
}
/**
* Returns an array of the fizzbuzz values from start to end, inclusive.
* For example, if the fizz number is 3 and buzz number is 4,
* getValues(2,6) should return an array of Strings:
* {"2", "fizz", "buzz", "5", "fizz"}
* @param start
* the number to start from; start > 0
* @param end
* the number to end at; end >= start
* @return the array of fizzbuzz values
*/
public String[] getValues(int start, int end) {
return new String[] {"1", "2", "fizz", "buzz"};
}
}
Here's the FizzCommander.java
package fizzbuzz;
import java.util.Scanner;
public class FizzCommander {
public static void main(String[] args) {
Scanner conIn = new Scanner(System.in);
try {
System.out.println("Enter a fizz number and a buzz number:");
final int fizz = conIn.nextInt();
final int buzz = conIn.nextInt();
FizzBuzz fb = new FizzBuzz(fizz, buzz);
int upper = 20;
System.out.println("FizzBuzz values of 1 to "+upper+":");
int i;
for(i=1;i<upper;i++) {
System.out.print(fb.getValue(i)+", ");
}
System.out.println(fb.getValue(i));
} finally {
conIn.close();
}
}
}
Here's the FizzBuzzTest.java:
package fizzbuzz;
import static org.junit.Assert.*;
import org.junit.Test;
public class FizzBuzzTest {
private final FizzBuzz ThreeFour = new FizzBuzz(3, 4);
private final FizzBuzz TwoFive = new FizzBuzz(2, 5);
@Test
public void testNotFizzOrBuzzTrivial() {
assertEquals("getValue returns incorrect value", "1",
ThreeFour.getValue(1));
assertEquals("getValue returns incorrect value", "2",
ThreeFour.getValue(2));
}
@Test
public void testFizzOrBuzzTrivial() {
assertEquals("getValue returns incorrect value", "fizz",
ThreeFour.getValue(3));
assertEquals("getValue returns incorrect value", "buzz",
ThreeFour.getValue(4));
}
@Test
public void testNotFizzOrBuzz() {
assertEquals("getValue returns incorrect value", "59",
ThreeFour.getValue(59));
assertEquals("getValue returns incorrect value", "3",
TwoFive.getValue(3));
assertEquals("getValue returns incorrect value", "143",
TwoFive.getValue(143));
}
@Test
public void testFizzOrBuzz() {
assertEquals("getValue returns incorrect value", "fizz",
ThreeFour.getValue(9));
assertEquals("getValue returns incorrect value", "buzz",
ThreeFour.getValue(8));
assertEquals("getValue returns incorrect value", "fizz",
ThreeFour.getValue(13));
assertEquals("getValue returns incorrect value", "buzz",
ThreeFour.getValue(116));
assertEquals("getValue returns incorrect value", "fizz",
ThreeFour.getValue(13125));
assertEquals("getValue returns incorrect value", "buzz",
TwoFive.getValue(59));
assertEquals("getValue returns incorrect value", "fizz",
TwoFive.getValue(29));
assertEquals("getValue returns incorrect value", "buzz",
TwoFive.getValue(11511));
assertEquals("getValue returns incorrect value", "fizz",
TwoFive.getValue(4342));
}
@Test
public void testFizzAndBuzz() {
assertEquals("getValue returns incorrect value", "fizzbuzz",
ThreeFour.getValue(12));
assertEquals("getValue returns incorrect value", "fizzbuzz",
ThreeFour.getValue(32));
assertEquals("getValue returns incorrect value", "fizzbuzz",
ThreeFour.getValue(45));
assertEquals("getValue returns incorrect value", "fizzbuzz",
TwoFive.getValue(100));
assertEquals("getValue returns incorrect value", "fizzbuzz",
TwoFive.getValue(521));
assertEquals("getValue returns incorrect value", "fizzbuzz",
ThreeFour.getValue(4310));
}
@Test
public void testGetValuesSimple() {
assertArrayEquals("getValues returns incorrect values", new String[] {
"1", "2", "fizz", "buzz", "5" }, ThreeFour.getValues(1, 5));
}
@Test
public void testGetValuesOffset() {
assertArrayEquals("getValues returns incorrect values", new String[] {
"2", "fizz", "buzz", "5", "fizz" },
ThreeFour.getValues(2, 6));
}
}
Can someone help me with this one please, I tried to debug for at least three hours but errors are still coming out.
Explanation / Answer
Here is the debugged code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
Note: I’m attaching only FizzBuzz.java as other files are unchanged.
// FizzBuzz.java
package fizzbuzz;
public class FizzBuzz {
private final int fizzNumber;
private final int buzzNumber;
/**
*
* Construct an object that can compute fizzbuzz values for a game of
*
* Fizz and Buzz.
*
* @param fizzNumber
* : an integer between 1 and 9
*
* @param buzzNumber
* : an integer between 1 and 9
*/
public FizzBuzz(int fizzNumber, int buzzNumber) {
this.fizzNumber = fizzNumber;
this.buzzNumber = buzzNumber;
}
/**
*
* Returns the fizzbuzz value for n. The rules are:
*
* - if n is divisible by fizzNumber, or contains the digit fizzNumber,
* return "fizz"
*
* - if n is divisible by buzzNumber, or contains the digit buzzNumber,
* return "buzz"
*
* - however, if both the above conditions are true, you must return
* "fizzbuzz"
*
* - if none of the above conditions is true, return the number itself.
*
* For example, getValue(1) returns "1".
*
* @param n
* : a positive integer
*
* @return the fizzbuzz value, as a String
*/
public String getValue(int n) {
// a variable to store the return value
String returnValue = "";
/**
* checking if n is divisible by fizz number or n contains fizz number
*/
if (n % fizzNumber == 0
|| Integer.toString(n).contains("" + fizzNumber)) {
// appending fizz to return value
returnValue += "fizz";
}
/**
* checking if n is divisible by buzz number or n contains buzz number
*/
if (n % buzzNumber == 0
|| Integer.toString(n).contains("" + buzzNumber)) {
// appending buzz to return value
returnValue += "buzz";
}
/**
* at this point, if return value is still empty, then the number is not
* fizz, buzz or fizzbuzz
*/
if (returnValue.equals("")) {
return Integer.toString(n); // return the number itself as a String
} else {
// the number is either fizz, buzz or fizzbuzz
return returnValue;
}
}
/**
*
* Returns an array of the fizzbuzz values from start to end, inclusive.
*
* For example, if the fizz number is 3 and buzz number is 4,
*
* getValues(2,6) should return an array of Strings:
*
* {"2", "fizz", "buzz", "5", "fizz"}
*
* @param start
*
* the number to start from; start > 0
*
* @param end
*
* the number to end at; end >= start
*
* @return the array of fizzbuzz values
*/
public String[] getValues(int start, int end) {
// creating an array with proper capacity
String array[] = new String[end - start + 1];
int number = start;
// looping through each locations in the array
for (int i = 0; i < array.length; i++) {
// finding the value of current number, adding to array
array[i] = getValue(number);
//moving to next number
number++;
}
return array;
}
}
/*OUTPUT of FizzCommander.java*/
Enter a fizz number and a buzz number:
3 4
FizzBuzz values of 1 to 20:
1, 2, fizz, buzz, 5, fizz, 7, buzz, fizz, 10, 11, fizzbuzz, fizz, buzz, fizz, buzz, 17, fizz, 19, buzz
/*JUnit test results*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.