CS102 : Lab Exam


Resources:
Warning:
  • During the exam, you are only allowed to access this page and the above two API sites.
  • At the end of the exam (which can be verified by timestamps of the log), you are allowed to access moodle site ONCE to submit your programs.
  • After the exam, network traffic history will be analyzed to check if you have complied with this requirement.

Note:
  • No separate design files are required, but you are still required to sufficiently comment your program logic.
  • If you have difficulty making the programs work, don't give up. You may still get significant partial credit for what you submit, such as a sound design of a program.
  • Each of the four questions are worth 25 points. There are two 5 (optional) point extra credit questions.


Exam:
  1. Arrays and Loops (25pts):
  2. Here is a skeleton for the Magic class. Your goal is to fill in the createOdd() method, correctly creating a magic square. The method will fill in the oddIntArray instance variable. Once your code works, you should be able to call createOdd for different sized magic squares by changing the ODDSIZE constant, and then you can see the output via System.out.println().
    • Remember that a "magic square" means that the sum of all rows, columns, and diagonals are the same.
    • Your method should make use of the following algorithm (from Wikipedia) which is slightly different from the one discussed in class.

      A method for constructing magic squares of odd order was published by the French diplomat de la Loubčre in his book A new historical relation of the kingdom of Siam (Du Royaume de Siam, 1693), under the chapter entitled The problem of the magical square according to the Indians. The method operates as follows:

      Starting from the central column of the first row with the number 1, the fundamental movement for filling the squares is diagonally up and right, one step at a time. If a filled square is encountered, one moves vertically down one square instead, then continuing as before. When a move would leave the square, it is wrapped around to the last row or first column, respectively.

      step 1
      1
      .
      .
      step 2
      1
      .
      2
      step 3
      1
      3
      2
      step 4
      1
      3
      4 2
      step 5
      1
      3 5
      4 2
      step 6
      1 6
      3 5
      4 2
      step 7
      1 6
      3 5 7
      4 2
      step 8
      8 1 6
      3 5 7
      4 2
      step 9
      8 1 6
      3 5 7
      4 9 2
  3. Recursion (25pts):
  4. Making magic squares for even numbered squares is more difficult. In this problem, you will fill in the createEven() method, which is a recursive method. The method takes in a single parameter: the next number to place in the square. The method will fill in the evenIntArray instance variable.
    • We will only consider a 4x4 magic square.
    • To make the program run faster, I suggest you create your array with the following values hard coded:
      3 2
      5 8
      6 7
      4 1
    • Extra Credit (5 points): What is the maximum number of times that the createEven() method is called when these 8 values are supplied? What is the maximum number of times that createEven would be called if no values were supplied (i.e., all 16 numbers needed to be placed)?


  5. Graphics (25pts):
  6. Fill in the drawSquare() method. The method takes in one parameter: the array you want to draw (either the odd or even magic square). You must show both the squares and the numbers within the squares. The method should work for a 4x4 magic square and 3x3, 5x5, 7x7, and 9x9 squares.
    • If you were unable to correctly create the magic squares, you may draw the squares with the default values (i.e., all -1).


  7. Animation (25pts):
  8. Rather than immediately drawing a magic square on the screen, add in the numbers one at a time, one per second. When you run the program, you will see an empty grid. After one second the number 1 will appear (in the correct location). After another second the number 2 will appear, etc.
    • If you were unable to correctly create the magic squares, you may draw the squares with the default values (i.e., all -1).
    • If you were unable to correctly draw the magic squares, you may repeatedly output the magic square via System.out.println. That is, for a 3x3 magic square, you would output 10 squares, 1 per second. The first square would consist of all -1's. The second square would have one 1 and the rest -1's. The third square would have one 1, one 2, and the rest -1's, etc.
    • Extra Credit (5pts): Make the animation look pretty. You could have the numbers fly into the grid from the side of the screen, the text could grow/shrink, the cell you're updating could flash colors, etc. Be creative!