My Students can't decide when to use:
for vs. for each loops,
while vs. do while loops,
static arrays vs. ArrayLists,
static methods vs. object methods
I don't know if this is helpful to anyone. If you find yourself in my shoes, I'm just trying to share a little project I hope you may find useful in your class. BTW, you are in my shoes if most of your students have Senioritis and are taking APCS as their first programming class....
So, I'm doing a review project with them now called Chess960 where we use the Random class to generate 960 Fischer Random chess boards.
Chess960 version 1 uses a static String array to represent the back row of the white pieces on the board. Also, we've been doing Object Oriented Programming all year, so I thought I'd take this opportunity to talk about static methods instead. As you can see in the following links, we used a combination of for loops, while loops and do while loops and made a big deal about when and how to use these different kinds of loops. We even talked about array traversal algorithms and linear searches.
Version 2 of Chess960 will do the same thing. However, I will be talking about refactoring code. In other words, how do we factor out unneeded code.
Version 3 will use an ArrayList to keep track of each randomly generated board until we get all combinations. Along with the ArrayList, I'll use enhanced for loops (aka for each loops) as well. Finally, I'll mention file I/O and have the entire ArrayList copied to a text file to print! That's one large ArrayList as each of the 960 entries consists of 8 Strings for a total of 7680 Strings. At 2 Bytes per String (unicode), that's 15.36KB or 15KiB in one variable holding a 2D matrix! Assuming 60 lines per page when printed and one board per line, that's 16 full pages when printed.
Note, there are only 960 combinations as we follow these rules to generate each board:
place a Bishop on a1, c1, e1 or g1,
place a Bishop on b1, d1, f1 or h1,
place the Queen on one of 6 remaining squares,
place a Knight on one of 5 remaining squares,
place a Knight on one of 4 remaining squares,
place a Rook on the first empty square,
place the King on the next empty square and
place a Rook on the last remaining square.
So, by the counting principal, we get 4*4*6*5*4*1 = 1920 ways to do this, right? No, it's 1920/2 = 960 because the Knights are considered repeated pieces. Note, the Bishops are distinct as a Bishop placed on a dark square (a1, c1, e1 or g1) will always stay on dark squares and a Bishop placed on a light square (b1, d1, f1 or h1) will always stay on light squares. Also, placing the Rook-King-Rook counts only once as the King has to be between the Rooks for castling!
Well, that's all folks!