Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Assignment18{
  3. char mat[][];
  4. int row, col;
  5.  
  6. Assignment18(int row, int col){
  7. mat = new char[this.row = row][this.col = col];
  8. }
  9.  
  10. void input(){
  11. Scanner sc = new Scanner(System.in);
  12. System.out.println("Input the elements of the array");
  13. for(int i = 0; i < row; i++){
  14. for(int j = 0; j < col; j++){
  15. System.out.printf("mat[%d][%d] = ", i, j);
  16. mat[i][j] = sc.nextLine().charAt(0);
  17. }
  18. }
  19. }
  20.  
  21. void generateMatrix(){
  22. char curr_ch = (char) 0;
  23. for(int i = 0; i < row; i++){
  24. for(int j = 0; j < col; j++){
  25. mat[i][j] = curr_ch;
  26. curr_ch += 1;
  27. }
  28. }
  29. }
  30.  
  31. void leftShift(){
  32. for(int i = 0; i < row; i++){
  33. // Perform left shift on ith row
  34. for(int j = 0; j < col - 1; j++){
  35. char temp = mat[i][j];
  36. mat[i][j] = mat[i][j + 1];
  37. mat[i][j + 1] = temp;
  38. }
  39. }
  40. }
  41.  
  42. void rightShift(){
  43. for(int i = 0; i < row; i++){
  44. // Perform right shift on ith row
  45. for(int j = col - 1; j >= 1; j--){
  46. char temp = mat[i][j];
  47. mat[i][j] = mat[i][j - 1];
  48. mat[i][j - 1] = temp;
  49. }
  50. }
  51. }
  52.  
  53. void upShift(){
  54. for(int j = 0; j < col; j++){
  55. // Perform upshift on jth column
  56. for(int i = 0; i < row - 1; i++){
  57. char temp = mat[i][j];
  58. mat[i][j] = mat[i + 1][j];
  59. mat[i + 1][j] = temp;
  60. }
  61. }
  62. }
  63.  
  64. void downShift(){
  65. for(int j = 0; j < col; j++){
  66. // Perform upshift on jth column
  67. for(int i = row - 1; i >= 1; i--){
  68. char temp = mat[i][j];
  69. mat[i][j] = mat[i - 1][j];
  70. mat[i - 1][j] = temp;
  71. }
  72. }
  73. }
  74.  
  75. void allDiagonalShift(){
  76. // Left Diagonal Shift
  77. int half = row / 2;
  78. for(int i = 0; i < half; i++){
  79. char temp = mat[i][i];
  80. mat[i][i] = mat[col - 1 - i][col - 1 - i];
  81. mat[col - 1 - i][col - 1 - i] = temp;
  82. }
  83.  
  84. // Right Diagonal Shift
  85. int index = 0;
  86. for(int i = col - 1; i >= half; i--){
  87. char temp = mat[index][i];
  88. mat[index][i] = mat[i][index];
  89. mat[i][index] = temp;
  90. index++;
  91. }
  92. }
  93.  
  94. char[] getCycle(char mat[][], int r_left, int c_left){
  95. int row = mat.length;
  96. int col = mat[0].length;
  97.  
  98. char boundary[] = new char[(2 * col) + (2 * row) - 4];
  99. int curr_r = r_left, curr_c = c_left, index = 0;
  100.  
  101. // Right
  102. for(int i = 0; i < col; i++){
  103. boundary[index] = mat[curr_r][i];
  104. index++;
  105. }
  106. // Down
  107. for(int i = 1; i < row; i++){
  108. boundary[index] = mat[i][col - 1];
  109. index++;
  110. }
  111. // Left
  112. for(int i = col - 2; i >= 0; i--){
  113. boundary[index] = mat[row - 1][i];
  114. index++;
  115. }
  116. // Up
  117. for(int i = row - 2; i >= 1; i--){
  118. boundary[index] = mat[i][curr_c];
  119. index++;
  120. }
  121. return boundary;
  122. }
  123.  
  124. char[] getCycle(int cycle_num){
  125. char temp[] = new char[row * col];
  126. int curr_r = cycle_num - 1, curr_c = cycle_num - 1, index = 0;
  127. int max_row = row - curr_r, max_col = col - curr_c;
  128.  
  129. // Right
  130. for(int i = curr_c; i < max_col; i++){
  131. temp[index] = mat[curr_r][i];
  132. index++;
  133. }
  134. // Down
  135. for(int i = curr_r + 1; i < max_row; i++){
  136. temp[index] = mat[i][max_col - 1];
  137. index++;
  138. }
  139. // Left
  140. for(int i = max_col - 2; i >= curr_c; i--){
  141. temp[index] = mat[max_row - 1][i];
  142. index++;
  143. }
  144. // Up
  145. for(int i = max_row - 2; i >= curr_r + 1; i--){
  146. temp[index] = mat[i][curr_c];
  147. index++;
  148. }
  149.  
  150. char boundary[] = new char[index];
  151. // Copying content from temp to boundary
  152. for(int i = 0; i < index; i++){
  153. boundary[i] = temp[i];
  154. }
  155.  
  156. return boundary;
  157. }
  158.  
  159. void insertCircular(char boundary[], int cycle_num){
  160. int curr_r = cycle_num - 1, curr_c = cycle_num - 1, index = 0;
  161. int max_row = row - curr_r, max_col = col - curr_c;
  162.  
  163. // Right
  164. for(int i = curr_c; i < max_col; i++){
  165. mat[curr_r][i] = boundary[index];
  166. index++;
  167. }
  168. // Down
  169. for(int i = curr_r + 1; i < max_row; i++){
  170. mat[i][max_col - 1] = boundary[index];
  171. index++;
  172. }
  173. // Left
  174. for(int i = max_col - 2; i >= curr_c; i--){
  175. mat[max_row - 1][i] = boundary[index];
  176. index++;
  177. }
  178. // Up
  179. for(int i = max_row - 2; i >= curr_r + 1; i--){
  180. mat[i][curr_c] = boundary[index];
  181. index++;
  182. }
  183. }
  184.  
  185. void cyclingShift(int cycle_num){
  186. char boundary[] = getCycle(cycle_num);
  187.  
  188. if(cycle_num % 2 == 1){
  189. // Clockwise Shift
  190. for(int i = boundary.length - 1; i >= 1; i--){
  191. char temp = boundary[i];
  192. boundary[i] = boundary[i - 1];
  193. boundary[i - 1] = temp;
  194. }
  195. }
  196. else{
  197. // Anti-Clockwise Shift
  198. for(int i = 0; i < boundary.length - 1; i++){
  199. char temp = boundary[i];
  200. boundary[i] = boundary[i + 1];
  201. boundary[i + 1] = temp;
  202. }
  203. }
  204.  
  205. insertCircular(boundary, cycle_num);
  206. }
  207.  
  208. void cyclingShift(){
  209. int half = row / 2;
  210. for(int cycle_num = 1; cycle_num <= half; cycle_num++){
  211. cyclingShift(cycle_num);
  212. }
  213. }
  214.  
  215. void display(){
  216. for(int i = 0; i < row; i++){
  217. for(int j = 0; j < col; j++){
  218. System.out.print(mat[i][j] + "\t");
  219. }
  220. System.out.println();
  221. }
  222. }
  223.  
  224. public static void main(String[]args){
  225. Assignment18 obj = new Assignment18(5, 5);
  226.  
  227. obj.input();
  228. System.out.println("Before");
  229. obj.display();
  230. obj.leftShift();
  231. System.out.println("\nAfter Left Shift");
  232. obj.display();
  233. obj.upShift();
  234. System.out.println("\nAfter Up Shift");
  235. obj.display();
  236. obj.rightShift();
  237. System.out.println("\nAfter Right Shift");
  238. obj.display();
  239. obj.downShift();
  240. System.out.println("\nAfter Down Shift");
  241. obj.display();
  242. obj.allDiagonalShift();
  243. System.out.println("\nAfter All Diagonal Shift");
  244. obj.display();
  245. obj.cyclingShift();
  246. System.out.println("\nAfter All Cycling Shifts");
  247. obj.display();
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement