Note that I added the "default version" since they wanted iterate
to accept 0 arguments. The following functions must be supplied exactly:
enter image description here
So, for these methods, comments regarding naming are unnecessary.
Also note that by the assignment question (unless I'm misinterpreting it), iterate
must print as it goes; it can't just return the results, which would be ideal.
Note that I added the "default version" since they wanted iterate
to accept 0 arguments.
Note that I added the "default version" since they wanted iterate
to accept 0 arguments. The following functions must be supplied exactly:
enter image description here
So, for these methods, comments regarding naming are unnecessary.
Also note that by the assignment question (unless I'm misinterpreting it), iterate
must print as it goes; it can't just return the results, which would be ideal.
FizzBuzz-like Assignment
I have an assignment for my Java programming class that is kind of an expanded FizzBuzz-like question:
- Write a program that iterates through numbers from 0 to 113 using a loop. Print the numbers, one number per line. As you print each number, say x, also print the following when appropriate, separated by commas:
- If the number is odd, print "x is odd"
- If the number is divisible by 5, print "hi five"
- If the total of a number (x) and its subsequent number (x+1) is a value divisible by 7, print "wow"
- If the number is prime, print "prime".
My main concerns are:
- Am I interpreting the condition for part 3 (
isWow
) correctly? - Is there a better way of organizing the function that doesn't require a cascade of
if
statements? - For an acedemic context, are the comments appropriate?
- Anything else that could be improved upon, including style.
package comp268.q9;
import java.util.ArrayList;
public class Number {
//I thought I might as well generalize the pattern.
public static boolean xIsDivisibleByY(int x, int y) {
if (y == 0) return false;
return x % y == 0;
}
public static boolean isDivisibleBy5(int n) {
return xIsDivisibleByY(n, 5);
}
public static boolean isDivisibleBy7(int n) {
return xIsDivisibleByY(n, 7);
}
public static boolean isOdd(int n) {
return !xIsDivisibleByY(n, 2);
}
public static boolean isWow(int n) {
return isDivisibleBy7(n + n + 1);
}
//A very simple prime checker.
//The only optimization I'm using is that I'm only checking factors up to
// the sqrt of x, since past there we're finding the "mirror" of factors
// we've already found.
public static boolean isPrime(int n) {
//Numbers <= 1 are not considered prime
if ( n < 2 ) return false;
//Caching the sqrt since it's relatively expensive to compute.
int sqrtOfN = (int)Math.sqrt(n) + 1;
for (int factorToCheck = 2; factorToCheck < sqrtOfN; factorToCheck++) {
if (xIsDivisibleByY(n, factorToCheck)) {
return false;
}
}
return true;
}
//I could just mutate the passed String.
//Avoiding side-effects seemed cleaner though.
private static String addToList(String str, String addition) {
return str + ", " + addition;
}
//The instructions suggest that the method should print the messages directly.
//To me, that seems like unnecessary side-effects since we're returning a list of messages.
//I added a flag parameter to control whether or not the method prints directly.
public static ArrayList<String> iterate(int start, int end, boolean printDirectly) {
ArrayList<String> allMessages = new ArrayList<String>();
for (int n = start; n <= end; n++) {
String curMessages = Integer.toString(n);
//The instructions suggest that the number itself should be included
// in the message, even if it adds redundancy; unless it's intent was
// for the message to literally be "x is odd".
if (isOdd(n)) {
curMessages = addToList(curMessages, n + " is odd");
}
if (isDivisibleBy5(n)) {
curMessages = addToList(curMessages,"hi five");
}
if (isWow(n)) {
curMessages = addToList(curMessages, "wow");
}
if (isPrime(n)) {
curMessages = addToList(curMessages, "prime");
}
if (printDirectly) {
System.out.println(curMessages);
}
allMessages.add(curMessages);
}
return allMessages;
}
//Default version to abide by the API
public static ArrayList<String> iterate() {
return iterate(0, 113, true);
}
}
Note that I added the "default version" since they wanted iterate
to accept 0 arguments.