People who know me are familiar with how, generally, I’m not too fond of clever solutions. This is because clever ones often are flawed in some subtle way that leads to bugs later. I prefer clear, precise solutions.
The situation below was a little different, and now I task you with solving the same problem in a similar, if not exact, way.
The other day a student asked me how to solve a problem that had to do with an identification system where the building was a letter and a digit, and the room was another digit. Each building had 50 rooms. The output had to look like the details below.
A-1-35 B-1-9 B-2-39 C-1-31 C-2-42 D-1-46 D-2-34 E-1-29 E-2-10 F-1-4 F-2-33 G-1-50 G-2-17 H-1-24 H-2-45 I-1-47 I-2-11 J-1-27 J-2-44 K-1-19 K-2-37 L-1-5 L-2-7 M-1-25 M-2-30 N-1-3 N-2-6
The underlying gist was we were to select one room in each building randomly. Note that there is no A-2. This building was closed. So, the output needed to reflect this. The original code solution was long and busy with many additional methods. We chipped away until we got to the bones of what you see below.
import java.util.ArrayList;
import java.util.Collections;
public class JCells {
public static void main(String[] args) {
ArrayList cells = new ArrayList<>();
int x;
for ( x = 0; x < 50; x++)
cells.add(x + 1);
Collections.shuffle(cells);
for ( x = 0; x < 28; x++) {
// only 2 statements! No more! And only ONE println!
}
}
}
Your task is to complete the second for
loop using only two or three lines of code and exactly one println()
or printf()
(your choice). Get the output above and email me your solution.