Advertisement
Crenox

Matrix Mirror Java Program

Apr 20th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. // Sammy Samkough
  2. // Matrix Mirror Class
  3. // Spec: Allows client to construct a matrix of any size
  4. // and perform various matrix mirroring operations
  5. // Note: A diagonal mirror requires a square matrix as a pre-condition
  6.  
  7. public class MatrixMirror
  8. {
  9. private int[][] mat;
  10.  
  11. /** Construct a square 3x3 matrix by default
  12. /* and invoke populateMatrixSequentially() to fill it
  13. */
  14. public MatrixMirror()
  15. {
  16. mat = new int[0][0];
  17. }
  18.  
  19. /** Construct a matrix with size rows x cols
  20. /* and invoke populateMatrixSequentially() to fill it
  21. */
  22. public MatrixMirror(int rows, int cols)
  23. {
  24. mat = new int[rows][cols];
  25. }
  26.  
  27. /** Fills mat with numbers starting with 1 in the upper left
  28. /* and continuing from left to right, row by row
  29. /* Ex 1: mat
  30. /* 1 2 3 4 5
  31. /* 6 7 8 9 10
  32. /* 11 12 13 14 15
  33. */
  34. public void populateMatrixSequentially()
  35. {
  36. int count = 1;
  37.  
  38. for (int r = 0; r < mat.length; r++)
  39. {
  40. for (int c = 0; c < mat[r].length; c++)
  41. {
  42. mat[r][c] = count;
  43. count ++;
  44. }
  45. }
  46. }
  47.  
  48. /** Use a vertical mirror down the center of a matrix to reflect the left half
  49. /* of the matrix onto the right.
  50. /* Ex 1: mat mat (after mirroring)
  51. /* 1 2 3 4 5 1 2 3 2 1
  52. /* 6 7 8 9 10 6 7 8 7 6
  53. /* 11 12 13 14 15 11 12 13 12 11
  54. /*
  55. /* Ex 2: mat mat (after mirroring)
  56. /* 1 2 3 4 1 2 2 1
  57. /* 5 6 7 8 5 6 6 5
  58. /* 9 10 11 12 9 10 10 9
  59. */
  60. public void mirrorVerticalLeftToRight()
  61. {
  62. for (int r = 0; r < mat.length; r++)
  63. {
  64. for (int c = 0; c < mat[r].length; c++)
  65. {
  66.  
  67. }
  68. }
  69. }
  70.  
  71. /** Use a horizontal mirror across the center of a matrix to reflect the top half
  72. /* of the matrix onto the bottom half.
  73. /* Ex 1: mat mat (after mirroring)
  74. /* 1 2 3 4 5 1 2 3 4 5
  75. /* 6 7 8 9 10 6 7 8 9 10
  76. /* 11 12 13 14 15 1 2 3 4 5
  77. /*
  78. /* Ex 2: mat mat (after mirroring)
  79. /* 1 2 3 4 1 2 3 4
  80. /* 5 6 7 8 5 6 7 8
  81. /* 9 10 11 12 5 6 7 8
  82. /* 13 14 15 16 1 2 3 4
  83. */
  84. public void mirrorHorizontalTopToBottom()
  85. {
  86. for (int r = 0; r < mat.length; r++)
  87. {
  88. for (int c = 0; c < mat[r].length; c++)
  89. {
  90.  
  91. }
  92. }
  93. }
  94.  
  95. /** Extra: Use a mirror across the major diagonal (top-left to bottom-right)
  96. /* and reflect the elements from left to right across this diagonal
  97. /* mat mat (after mirroring)
  98. /* 1 2 3 4 1 5 9 13
  99. /* 5 6 7 8 5 6 10 14
  100. /* 9 10 11 12 9 10 11 15
  101. /* 13 14 15 16 13 14 15 16
  102. /* Precondition: Must be a square matrix
  103. */
  104. public void mirrorMajorDiagonalLeftToRight()
  105. {
  106. for (int r = 0; r < mat.length; r++)
  107. {
  108. for (int c = 0; c < mat[r].length; c++)
  109. {
  110.  
  111. }
  112. }
  113. }
  114.  
  115. /** Extra: Use a mirror across the minor diagonal (top-right to bottom-left)
  116. /* and reflect the elements from left to right across this diagonal
  117. /* mat mat (after mirroring)
  118. /* 1 2 3 4 1 2 3 4
  119. /* 5 6 7 8 5 6 7 3
  120. /* 9 10 11 12 9 10 6 2
  121. /* 13 14 15 16 13 9 5 1
  122. */
  123. public void mirrorMinorDiagonalLeftToRight()
  124. {
  125. for (int r = 0; r < mat.length; r++)
  126. {
  127. for (int c = 0; c < mat[r].length; c++)
  128. {
  129.  
  130. }
  131. }
  132. }
  133.  
  134. /** @return the matrix with tabs between elements
  135. /* and a new line at the end of each row
  136. */
  137. public String toString()
  138. {
  139. String result = "";
  140.  
  141. return result;
  142. }
  143. }
  144. ----------------------------------------------------------------------------------------------------------------------------
  145. // Sammy Samkough
  146. // Matrix Mirror Class
  147. // Spec: Allows client to construct a matrix of any size
  148. // and perform various matrix mirroring operations
  149. // Note: A diagonal mirror requires a square matrix as a pre-condition
  150.  
  151. public class MatrixMirrorClient
  152. {
  153. public static void main(String[] args)
  154. {
  155. MatrixMirror mm = new MatrixMirror(5,4);
  156.  
  157. mm.mirrorVerticalLeftToRight();
  158. System.out.println(mm);
  159.  
  160. mm.populateMatrixSequentially();
  161. mm.mirrorHorizontalTopToBottom();
  162. System.out.println(mm);
  163.  
  164. /***** Uncomment if you have implemented the extra diagonal mirrors
  165. mm = new MatrixMirror(4,4);
  166. mm.mirrorMajorDiagonalLeftToRight();
  167. System.out.println(mm);
  168.  
  169. mm = new MatrixMirror(4,4);
  170. mm.mirrorMinorDiagonalLeftToRight();
  171. System.out.println(mm);
  172. ******/
  173. }
  174. }
  175. /*
  176.  
  177. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement