Last evening watched the Super Bowl LI game, halftime show and commercials. I am not a sports fan. That said; the game was amazing. The halftime show was very good. Not sure about the commercials. An effective commercial is one that you remember and takes you to the advertised product. Do not seem to recall any ad that fitted such description.
For some reason last night I was thinking about the FizzBuzz post. The code seems to work but I believe I fell short from describing what was going on and perhaps why was it of value. Would like it to revisit the same example but with a different name and similar code.
The new requirements are: Traverse an ordered set of integers from 1 <= N <= 50. For each number do the following: If the number is divisible by 3 print the word “Johnny”. If the number is divisible by 5 print the word “Cake”. If the number is both divisible by 3 and 5 print “Johnny Cake”. Otherwise print the number.
I chose Johnny Cake because in the city I live there is a road named Johnny Cake. After driving on it a few times I decided to search for the origins of the name. It is related to a cornmeal flatbread created by the early inhabitants of the East Coast in North America. You can Google more about the history of the name.
What is interesting to note is that based on the requirements one can generate the following truth table:
Divisible by 5 | Divisible by 3 | Action |
False | False | Print n |
False | True | Print “Johnny” |
True | False | Print “Cake” |
True | True | Print “Johnny Cake” |
In this case the table contains four states because we are using two variables. I have encountered this situation several times using two and three variables (8 cases). The approach seems to be the same (just like a design pattern).
The output from the console of the Eclipse IDE follows:
1
2
Johnny
4
Cake
Johnny
7
8
Johnny
Cake
11
Johnny
13
14
Johnny Cake
16
17
Johnny
19
Cake
Johnny
22
23
Johnny
Cake
26
Johnny
28
29
Johnny Cake
31
32
Johnny
34
Cake
Johnny
37
38
Johnny
Cake
41
Johnny
43
44
Johnny Cake
46
47
Johnny
49
Cake
The code written in Java follows:
public class Solution {
public static void main(String[] args) {
// **** ****
final int START_VAL = 1;
final int END_VAL = 50;
boolean f, t;
String j = “Johnny”,
c = “Cake”,
s;
// **** loop through numbers in range 1 to 100 ****
for (int n = START_VAL; n <= END_VAL; n++) {
// **** set the values for the truth table ****
t = ((n % 3) == 0) ? true : false;
f = ((n % 5) == 0) ? true : false;
// **** process the truth table ****
if (f == false) {
// **** false false ****
if (t == false) {
s = Integer.toString(n);
}
// **** false true ****
else {
s = j;
}
} else {
// **** true false ****
if (t == false) {
s = c;
}
// **** true true ****
else {
s = j + ” ” + c;
}
}
// **** display results ****
System.out.println(s);
}
}
}
If you have comments or questions regarding this or any other entry in this blog, please do not hesitate and send me a message. I will reply ASAP and will not use your name in a post unless you explicitly allow me to do so.
John
john.canessa@gmail
Follow me on Twitter: @john_canessa