You will need to complete its generateArray() method . The method should return
ID: 3890656 • Letter: Y
Question
You will need to complete its generateArray() method. The method should return an array of String with a length of numEntries. For each entry in the array, assign it a value using the "FizzBuzz" rules specified in the method comments. public class FizzBuzzArray { /** * This returns a String array of the specified length. Each entry will store a String using the specified rules: * <ul> * <li>For the entry at index 0, store "Empty"</li> * <li>For entries whose index is a multiple of 3, store "Fizz"</li> * <li>For entries whose index is a multiple of 5, store "Buzz"</li> * <li>For entries whose index is a multiple of 3 AND 5, store "FizzBuzz"</li> * <li>For all other entries, store the index as a string (e.g., "2" or "6").</li> * </ul> * * @param numEntries Length of the array to be returned * @return Array of the appropriate length with String entries appropriate for a FizzBuzz test. */ public static String[] generateArray(int numEntries) { } }
Explanation / Answer
import java.io.*;
public class FizzBuzzArray{
public static String[] generateArray(int numEntries){
String[] data = new String[numEntries];
for (int i = 0; i<numEntries; i++){
data[i] = new String();
if (i==0){
data[i] = "Empty";
continue;
}
data[i] = String.valueOf(i);
if (i%3 == 0){
data[i] = "Fizz";
}
if (i%5 == 0){
data[i] = "Buzz";
}
if (i%5 == 0 && i%3 == 0){
data[i] = "FizzBuzz";
}
}
return data;
}
public static void main(String[] args){
String[] a;
a = generateArray(20);
for (int i = 0; i<20; i++)
System.out.println(a[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.