akosiraff

Assignment 10 Matrix JAVA

Apr 29th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/assignment-10-matrix-java/
  3.  
  4. CSCI 2311, Spring 2013 Programming Assignment 10
  5. The program is due Tuesday, April 30 by midnight.
  6. Overview of Assignment
  7. Begin this assignment by first creating a new Java Project called Assignment 10. This assignment has two parts. The first part is used to understand multidimensional arrays, and the second part is used to understand recursion and error checking which we will go over Thursday, April 18.
  8. Don’t forget you still need javadocs for all your methods and classes.
  9. Part 1
  10. I provide the class with the main method for you called MatrixMain. This is the class I will use to test your Matrix class and you should NOT change it, except for testing purposes.
  11. Remember from one of your previous assignments that a matrix is a grid, with each location in the grid containing some information. For our purposes, we will assume these values are integers. For example, we can have the following matrix:
  12. 8 9 4
  13. 5 1 0
  14. This is a 2 x 3 matrix, meaning it has 2 rows and 3 columns. Two-dimensional arrays are a great way to represent a matrix. If we wanted to create a two dimensional array for the matrix shown above, we could do the following:
  15. int[][] someArray = { {8, 9, 4},
  16. {5, 1, 0} };
  17. If we wanted to access the value at a given position, we can use its row and column to return the value. For example, someArray[1][1] represents the value 1 since it is in row 1, column 1 and someArray[0][2] represents the value 4 since it is in row 0 and column 2.
  18. In the special case of using a two-dimensional array to represent a matrix, the number of rows in the two-dimensional array shown above would be someArray.length, and the number of columns would be someArray[0].length, since all rows have the same number of columns in a matrix.
  19. I provide the template for the Matrix class. Use it!
  20. Matrix
  21. - matrix: int[ ][ ]
  22. + Matrix( int[ ][ ] m )
  23. + rows(): int
  24. + columns(): int
  25. + getValue( int row, int column ): int
  26. + setValue( int value, int row, int column ): void
  27. + transpose(): Matrix
  28. + display(): void
  29. + equals( Matrix m ): boolean
  30. - areSameSize( Matrix m ): boolean
  31. + randomMatrix( int rows, int columns ): static Matrix
  32. - matrix: int[ ][ ]
  33. A two-dimensional integer array used to store the values of the matrix.
  34. + Matrix( int[ ][ ] m )
  35. Matrix constructor. First, use the matrix instance variable to create a new array with the same row and columns as the parameter array m. Next, create a loop to copy all of the values in the m array, into the matrix array. You SHOULD NOT simply set matrix = m.
  36. + rows(): int
  37. Returns the number of rows for the matrix. Remember that rows represent how many arrays are in the two-dimensional array.
  38. + columns(): int
  39. Returns the number of columns for the matrix. Remember that columns represent how many array elements are in a particular array of the two-dimensional array (which in this case, all columns will be the same size; i.e., not ragged)
  40. + getValue( int row, int column ): int
  41. Returns the integer value at the specified row and column in the matrix array.
  42. + setValue( int value, int row, int column ): void
  43. Sets the integer value at the specified row and column in the matrix array equal to the parameter value.
  44. + transpose(): Matrix
  45. Returns a new instance of Matrix with its matrix value transposed. The new instance of Matrix MUST have its own two-dimensional array, meaning you will need to create a new two-dimensional array and copy the values in matrix into the correct locations in this new array.
  46. To transpose a matrix, we switch the rows to the columns and the columns to the rows. Consider the following 3 x 2 matrix:
  47. 1 3
  48. 2 4
  49. 0 6
  50. If we transpose it, we are left with the following 2 x 3 matrix:
  51. 1 2 0
  52. 3 4 6
  53. + display(): void
  54. Displays to the console the rows and columns of the matrix.
  55. + equals( Matrix m ): boolean
  56. Returns true if the parameter Matrix m has the exact same elements as the invoking Matrix; otherwise, it returns false. This method should start by invoking the helper method areSameSize() size, since two matrices of different sizes cannot be equal.
  57. - areSameSize( Matrix m ): boolean
  58. Returns true if the parameter Matrix m and the invoking Matrix have the same number of rows and columns; otherwise, returns false.
  59. + randomMatrix( int rows, int columns ): static Matrix
  60. Returns a new instance of Matrix with a brand new two-dimensional array, with the rows and columns specified from the parameters. Each value in the array is a randomly generated integer from 0 to 9. Remember you can use the nextInt() method of the Random class to do this; i.e., nextInt(10) returns a value from 0 to 9 inclusively.
  61. Tips
  62. In the main method, you will notice I first get the user input as a String, and then try to convert the String to an integer. You must, do this same thing when you introduce error checking or you may get unexpected results. Make sure you only get user input using the nextLine() method as I am currently doing.
  63. Example Output (User input in bold)
  64. Constant matrix (2 x 3):
  65. 1 2 3
  66. 4 5 6
  67. How many rows should the random matrix have: 3
  68. How many columns should the random matrix have: 5
  69. Random matrix (3 x 5):
  70. 9 0 2 7 2
  71. 5 6 3 6 9
  72. 1 6 0 2 6
  73. Getting each value:
  74. [0, 0] = 9 [0, 1] = 0 [0, 2] = 2 [0, 3] = 7 [0, 4] = 2
  75. [1, 0] = 5 [1, 1] = 6 [1, 2] = 3 [1, 3] = 6 [1, 4] = 9
  76. [2, 0] = 1 [2, 1] = 6 [2, 2] = 0 [2, 3] = 2 [2, 4] = 6
  77. Transpose of matrix (5 x 3):
  78. 9 5 1
  79. 0 6 6
  80. 2 3 0
  81. 7 6 2
  82. 2 9 6
  83. Incremented transpose values:
  84. [0, 0] = 10 [0, 1] = 6 [0, 2] = 2
  85. [1, 0] = 1 [1, 1] = 7 [1, 2] = 7
  86. [2, 0] = 3 [2, 1] = 4 [2, 2] = 1
  87. [3, 0] = 8 [3, 1] = 7 [3, 2] = 3
  88. [4, 0] = 3 [4, 1] = 10 [4, 2] = 7
  89. Equality of matrices:
  90. - Constant equals Constant: true
  91. - Constant equals Random: false
  92. - Random equals Transpose: false
  93. - Random equals Random: true
  94. Part 2
  95. I provide the template for the Recursion class. Use it!
  96. Recursion
  97. + main( String[] args ): static void
  98. + rangeSummationI( int start, int end ): static int
  99. + rangeSummationR( int start, int end): static int
  100. + hanoiMoves( int n, int start, int end, int extra ): static void
  101. + main( String[] args ): static void
  102. Main method. This method should be left unchanged except the code between the comments // error check start and // error check end. When the user inputs a number of disks, you need to make sure you catch any errors (such as if the user typed in a non-integer character) and also validate that they entered an integer greater than 0. You should continue to loop asking for input until a valid response is given. No input given for the number of disks should crash or terminate your program. You do NOT need to error check the starting range or ending range.
  103. + rangeSummationI( int start, int end ): static int
  104. Iteratively finds the summation of the values in the range from start to end. If the value of start is greater than or equal to end, this method simply returns the value of start.
  105. + rangeSummationR( int start, int end): static int
  106. Recursively finds the summation of the values in the range from start to end. If the value of start is greater than or equal to end, this method simply returns the value of start. To recursively solve this problem, add the value of start to the value returned from invoking this method with the range start + 1 to end.
  107. + hanoiMoves( int n, int start, int end, int extra ): static void
  108. Displays the solution to the Towers of Hanoi puzzle.
  109. The puzzle has three posts and some number n of disks. Each disk has a hole in the middle, so that it can be put on a post, and the disks are of different sizes.
  110. Initially, the disks are all stacked on the leftmost post, with larger disks closer to the bottom and smaller disks closer to the top. For example, if there are three disks, the puzzle starts out looking like this.
  111. The object is to move all of the disks from the leftmost post to the rightmost post. So when the puzzle is solved, it looks like this.
  112. But there are some rules that must be followed.
  113. 1. Only one disk can be moved at a time. The other disks must be on posts.
  114. 2. A disk can only be put down by placing it on one of the posts. For example, you can’t put a disk on the table beside the puzzle.
  115. 3. No disk can ever be put on top of a smaller disk. You can only put a disk on top of one that is larger than it.
  116. It is convenient to refer to the posts by names, such as L, M and R, for left, middle and right. It is also convenient to number the disk from smallest to largest; from 1 to n.
  117. To complete this method, follow these steps exactly:
  118. If n <= 0, there is no need to do anything. If n > 0, then break the problem down into the following steps:
  119. 1. Recursively call the method to move n-1 disks from start to extra, using end for storage. That stores the top n-1 disks out of the way, on the extra post, exposing the n-th disk on the start post.
  120. 2. Write one console output line saying
  121. Move disk from to .
  122. 3. Finally, recursively call the method again to move n-1 disks from extra to end, using start as storage. That moves those disks from storage to the end, where they are now on top of the n-th disk.
  123. Example Output 1 (User input in bold)
  124. What is our starting range: 1
  125. What is our ending range: 5
  126. Iterative range summation: 15
  127. Recursive range summation: 15
  128. How many disks should we use for Hanoi problem: 3
  129. Solution:
  130. Move disk 1 from L to R.
  131. Move disk 2 from L to M.
  132. Move disk 1 from R to M.
  133. Move disk 3 from L to R.
  134. Move disk 1 from M to L.
  135. Move disk 2 from M to R.
  136. Move disk 1 from L to R.
  137. Example Output 2 (User input in bold)
  138. What is our starting range: 17
  139. What is our ending range: 5
  140. Iterative range summation: 17
  141. Recursive range summation: 17
  142. How many disks should we use for Hanoi problem: -4
  143. Invalid! Input must be greater than 0.
  144. How many disks should we use for Hanoi problem: 7 dfgdfg dgdfgd
  145. EXCEPTION: Invalid input.
  146. How many disks should we use for Hanoi problem: sdfdfsdf
  147. EXCEPTION: Invalid input.
  148. How many disks should we use for Hanoi problem: 0
  149. Invalid! Input must be greater than 0.
  150. How many disks should we use for Hanoi problem: 4
  151. Solution:
  152. Move disk 1 from L to M.
  153. Move disk 2 from L to R.
  154. Move disk 1 from M to R.
  155. Move disk 3 from L to M.
  156. Move disk 1 from R to L.
  157. Move disk 2 from R to M.
  158. Move disk 1 from L to M.
  159. Move disk 4 from L to R.
  160. Move disk 1 from M to R.
  161. Move disk 2 from M to L.
  162. Move disk 1 from R to L.
  163. Move disk 3 from M to R.
  164. Move disk 1 from L to M.
  165. Move disk 2 from L to R.
  166. Move disk 1 from M to R.
  167. Assignment Submission
  168. Submit the following file(s) to me on blackboard:
  169. Matrix.java
  170. Recursion.java
  171. All assignments will be submitted through blackboard. You are only allowed ONE submission so do not submit the assignment until you are completely done with it. However, you can upload the assignment and save a draft without submitting it. If you do this, don’t forget to submit it before the due date. Saving a draft and submitting it are not the same thing.
  172. To submit the assignment, login to http://blackboard.ecu.edu. Navigate to our course and then to the “Assignments” folder. Click the link that says “Lab Assignment 10”. On this new page, scroll down to where it says “Attach File” and attach the java files for your program. You will need to browse to the location where you saved the file on your computer. If you aren’t sure where it is located, refer to assignment 1 on how to find out.
  173. Download: http://solutionzip.com/downloads/assignment-10-matrix-java/
Advertisement
Add Comment
Please, Sign In to add comment