Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Matrix Mirror Class
- // Spec: Allows client to construct a matrix of any size
- // and perform various matrix mirroring operations
- // Note: A diagonal mirror requires a square matrix as a pre-condition
- public class MatrixMirror
- {
- private int[][] mat;
- /** Construct a square 3x3 matrix by default
- /* and invoke populateMatrixSequentially() to fill it
- */
- public MatrixMirror()
- {
- mat = new int[0][0];
- }
- /** Construct a matrix with size rows x cols
- /* and invoke populateMatrixSequentially() to fill it
- */
- public MatrixMirror(int rows, int cols)
- {
- mat = new int[rows][cols];
- }
- /** Fills mat with numbers starting with 1 in the upper left
- /* and continuing from left to right, row by row
- /* Ex 1: mat
- /* 1 2 3 4 5
- /* 6 7 8 9 10
- /* 11 12 13 14 15
- */
- public void populateMatrixSequentially()
- {
- int count = 1;
- for (int r = 0; r < mat.length; r++)
- {
- for (int c = 0; c < mat[r].length; c++)
- {
- mat[r][c] = count;
- count ++;
- }
- }
- }
- /** Use a vertical mirror down the center of a matrix to reflect the left half
- /* of the matrix onto the right.
- /* Ex 1: mat mat (after mirroring)
- /* 1 2 3 4 5 1 2 3 2 1
- /* 6 7 8 9 10 6 7 8 7 6
- /* 11 12 13 14 15 11 12 13 12 11
- /*
- /* Ex 2: mat mat (after mirroring)
- /* 1 2 3 4 1 2 2 1
- /* 5 6 7 8 5 6 6 5
- /* 9 10 11 12 9 10 10 9
- */
- public void mirrorVerticalLeftToRight()
- {
- for (int r = 0; r < mat.length; r++)
- {
- for (int c = 0; c < mat[r].length; c++)
- {
- }
- }
- }
- /** Use a horizontal mirror across the center of a matrix to reflect the top half
- /* of the matrix onto the bottom half.
- /* Ex 1: mat mat (after mirroring)
- /* 1 2 3 4 5 1 2 3 4 5
- /* 6 7 8 9 10 6 7 8 9 10
- /* 11 12 13 14 15 1 2 3 4 5
- /*
- /* Ex 2: mat mat (after mirroring)
- /* 1 2 3 4 1 2 3 4
- /* 5 6 7 8 5 6 7 8
- /* 9 10 11 12 5 6 7 8
- /* 13 14 15 16 1 2 3 4
- */
- public void mirrorHorizontalTopToBottom()
- {
- for (int r = 0; r < mat.length; r++)
- {
- for (int c = 0; c < mat[r].length; c++)
- {
- }
- }
- }
- /** Extra: Use a mirror across the major diagonal (top-left to bottom-right)
- /* and reflect the elements from left to right across this diagonal
- /* mat mat (after mirroring)
- /* 1 2 3 4 1 5 9 13
- /* 5 6 7 8 5 6 10 14
- /* 9 10 11 12 9 10 11 15
- /* 13 14 15 16 13 14 15 16
- /* Precondition: Must be a square matrix
- */
- public void mirrorMajorDiagonalLeftToRight()
- {
- for (int r = 0; r < mat.length; r++)
- {
- for (int c = 0; c < mat[r].length; c++)
- {
- }
- }
- }
- /** Extra: Use a mirror across the minor diagonal (top-right to bottom-left)
- /* and reflect the elements from left to right across this diagonal
- /* mat mat (after mirroring)
- /* 1 2 3 4 1 2 3 4
- /* 5 6 7 8 5 6 7 3
- /* 9 10 11 12 9 10 6 2
- /* 13 14 15 16 13 9 5 1
- */
- public void mirrorMinorDiagonalLeftToRight()
- {
- for (int r = 0; r < mat.length; r++)
- {
- for (int c = 0; c < mat[r].length; c++)
- {
- }
- }
- }
- /** @return the matrix with tabs between elements
- /* and a new line at the end of each row
- */
- public String toString()
- {
- String result = "";
- return result;
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Matrix Mirror Class
- // Spec: Allows client to construct a matrix of any size
- // and perform various matrix mirroring operations
- // Note: A diagonal mirror requires a square matrix as a pre-condition
- public class MatrixMirrorClient
- {
- public static void main(String[] args)
- {
- MatrixMirror mm = new MatrixMirror(5,4);
- mm.mirrorVerticalLeftToRight();
- System.out.println(mm);
- mm.populateMatrixSequentially();
- mm.mirrorHorizontalTopToBottom();
- System.out.println(mm);
- /***** Uncomment if you have implemented the extra diagonal mirrors
- mm = new MatrixMirror(4,4);
- mm.mirrorMajorDiagonalLeftToRight();
- System.out.println(mm);
- mm = new MatrixMirror(4,4);
- mm.mirrorMinorDiagonalLeftToRight();
- System.out.println(mm);
- ******/
- }
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement