Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Assignment18{
- char mat[][];
- int row, col;
- Assignment18(int row, int col){
- mat = new char[this.row = row][this.col = col];
- }
- void input(){
- Scanner sc = new Scanner(System.in);
- System.out.println("Input the elements of the array");
- for(int i = 0; i < row; i++){
- for(int j = 0; j < col; j++){
- System.out.printf("mat[%d][%d] = ", i, j);
- mat[i][j] = sc.nextLine().charAt(0);
- }
- }
- }
- void generateMatrix(){
- char curr_ch = (char) 0;
- for(int i = 0; i < row; i++){
- for(int j = 0; j < col; j++){
- mat[i][j] = curr_ch;
- curr_ch += 1;
- }
- }
- }
- void leftShift(){
- for(int i = 0; i < row; i++){
- // Perform left shift on ith row
- for(int j = 0; j < col - 1; j++){
- char temp = mat[i][j];
- mat[i][j] = mat[i][j + 1];
- mat[i][j + 1] = temp;
- }
- }
- }
- void rightShift(){
- for(int i = 0; i < row; i++){
- // Perform right shift on ith row
- for(int j = col - 1; j >= 1; j--){
- char temp = mat[i][j];
- mat[i][j] = mat[i][j - 1];
- mat[i][j - 1] = temp;
- }
- }
- }
- void upShift(){
- for(int j = 0; j < col; j++){
- // Perform upshift on jth column
- for(int i = 0; i < row - 1; i++){
- char temp = mat[i][j];
- mat[i][j] = mat[i + 1][j];
- mat[i + 1][j] = temp;
- }
- }
- }
- void downShift(){
- for(int j = 0; j < col; j++){
- // Perform upshift on jth column
- for(int i = row - 1; i >= 1; i--){
- char temp = mat[i][j];
- mat[i][j] = mat[i - 1][j];
- mat[i - 1][j] = temp;
- }
- }
- }
- void allDiagonalShift(){
- // Left Diagonal Shift
- int half = row / 2;
- for(int i = 0; i < half; i++){
- char temp = mat[i][i];
- mat[i][i] = mat[col - 1 - i][col - 1 - i];
- mat[col - 1 - i][col - 1 - i] = temp;
- }
- // Right Diagonal Shift
- int index = 0;
- for(int i = col - 1; i >= half; i--){
- char temp = mat[index][i];
- mat[index][i] = mat[i][index];
- mat[i][index] = temp;
- index++;
- }
- }
- char[] getCycle(char mat[][], int r_left, int c_left){
- int row = mat.length;
- int col = mat[0].length;
- char boundary[] = new char[(2 * col) + (2 * row) - 4];
- int curr_r = r_left, curr_c = c_left, index = 0;
- // Right
- for(int i = 0; i < col; i++){
- boundary[index] = mat[curr_r][i];
- index++;
- }
- // Down
- for(int i = 1; i < row; i++){
- boundary[index] = mat[i][col - 1];
- index++;
- }
- // Left
- for(int i = col - 2; i >= 0; i--){
- boundary[index] = mat[row - 1][i];
- index++;
- }
- // Up
- for(int i = row - 2; i >= 1; i--){
- boundary[index] = mat[i][curr_c];
- index++;
- }
- return boundary;
- }
- char[] getCycle(int cycle_num){
- char temp[] = new char[row * col];
- int curr_r = cycle_num - 1, curr_c = cycle_num - 1, index = 0;
- int max_row = row - curr_r, max_col = col - curr_c;
- // Right
- for(int i = curr_c; i < max_col; i++){
- temp[index] = mat[curr_r][i];
- index++;
- }
- // Down
- for(int i = curr_r + 1; i < max_row; i++){
- temp[index] = mat[i][max_col - 1];
- index++;
- }
- // Left
- for(int i = max_col - 2; i >= curr_c; i--){
- temp[index] = mat[max_row - 1][i];
- index++;
- }
- // Up
- for(int i = max_row - 2; i >= curr_r + 1; i--){
- temp[index] = mat[i][curr_c];
- index++;
- }
- char boundary[] = new char[index];
- // Copying content from temp to boundary
- for(int i = 0; i < index; i++){
- boundary[i] = temp[i];
- }
- return boundary;
- }
- void insertCircular(char boundary[], int cycle_num){
- int curr_r = cycle_num - 1, curr_c = cycle_num - 1, index = 0;
- int max_row = row - curr_r, max_col = col - curr_c;
- // Right
- for(int i = curr_c; i < max_col; i++){
- mat[curr_r][i] = boundary[index];
- index++;
- }
- // Down
- for(int i = curr_r + 1; i < max_row; i++){
- mat[i][max_col - 1] = boundary[index];
- index++;
- }
- // Left
- for(int i = max_col - 2; i >= curr_c; i--){
- mat[max_row - 1][i] = boundary[index];
- index++;
- }
- // Up
- for(int i = max_row - 2; i >= curr_r + 1; i--){
- mat[i][curr_c] = boundary[index];
- index++;
- }
- }
- void cyclingShift(int cycle_num){
- char boundary[] = getCycle(cycle_num);
- if(cycle_num % 2 == 1){
- // Clockwise Shift
- for(int i = boundary.length - 1; i >= 1; i--){
- char temp = boundary[i];
- boundary[i] = boundary[i - 1];
- boundary[i - 1] = temp;
- }
- }
- else{
- // Anti-Clockwise Shift
- for(int i = 0; i < boundary.length - 1; i++){
- char temp = boundary[i];
- boundary[i] = boundary[i + 1];
- boundary[i + 1] = temp;
- }
- }
- insertCircular(boundary, cycle_num);
- }
- void cyclingShift(){
- int half = row / 2;
- for(int cycle_num = 1; cycle_num <= half; cycle_num++){
- cyclingShift(cycle_num);
- }
- }
- void display(){
- for(int i = 0; i < row; i++){
- for(int j = 0; j < col; j++){
- System.out.print(mat[i][j] + "\t");
- }
- System.out.println();
- }
- }
- public static void main(String[]args){
- Assignment18 obj = new Assignment18(5, 5);
- obj.input();
- System.out.println("Before");
- obj.display();
- obj.leftShift();
- System.out.println("\nAfter Left Shift");
- obj.display();
- obj.upShift();
- System.out.println("\nAfter Up Shift");
- obj.display();
- obj.rightShift();
- System.out.println("\nAfter Right Shift");
- obj.display();
- obj.downShift();
- System.out.println("\nAfter Down Shift");
- obj.display();
- obj.allDiagonalShift();
- System.out.println("\nAfter All Diagonal Shift");
- obj.display();
- obj.cyclingShift();
- System.out.println("\nAfter All Cycling Shifts");
- obj.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement