Monday, May 24, 2010

How would you write this program in Java?

Suppose you are asked to write a program that models a Chess Board, which we know has rows and columns. Create a design for a two dimensional array that shows the piece that is on the particular square of the chesboard. Design an algorithm that iterates through each square on the chessboard and prints out what pice, if any, is on the board.

How would you write this program in Java?
I would create a two dimensional array of objects representing the squares with composition for a piece object. To iterate through, I would use two nested loops, one to represent the row and the other to represent the column of the matrix. As you iterate through, check to see if a square contains a piece object and print it with its positon and continue.


No comments:

Post a Comment