Class 11 Class 12

Shift Matrix ISC 2016

A class Shift contains a two-dimensional integer array of order (m × n) where the maximum values of both m and n is 5. Design the class Shift to shuffle the matrix (i.e. the first row becomes the last, the second row becomes the first and so on). The details of the members of the class are given below:

Class name: Shift
Data members/instance variables:
mat[][]: stores the array element.
m: integer to store the number of rows.
n: integer to store the number of columns.
Member functions/methods:
Shift(int mm, int nn): parameterized constructor to initialize the data members m = mm and n = nn.
void input(): enters the elements of the array.
void cyclic(Shift P): enables the matrix of the object P to shift each row upwards in a cyclic manner and store the resultant matrix in the current object.
void display(): displays the matrix elements.

Specify the class Shift giving details of the constructor, void input(), void cyclic(Shift) and void display(). Define the main() function to create an object and call the methods accordingly to enable the task of shifting the array elements.

class Shift{
    private int mat[][];
    private int m;
    private int n;
    public Shift(int mm, int nn){
        m = mm;
        n = nn;
        if(m > 5)
            m = 5;
        if(n > 5)
            n = 5;
        mat = new int[m][n];
    public void input()throws IOException{
        InputStreamReader in = new InputStreamReader(;
        BufferedReader br = new BufferedReader(in);
        System.out.println("Enter matrix elements:");
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                mat[i][j] = Integer.parseInt(br.readLine());
    public void cyclic(Shift P){
        int s = m - 1;
        int t = 0;
        for(int i = 0; i < n; i++)
            this.mat[s][t++] = P.mat[0][i];
        s = 0;
        for(int i = 1; i < m; i++){
            t = 0;
            for(int j = 0; j < n; j++)
                this.mat[s][t++] = P.mat[i][j];
    public void display(){
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++)
                System.out.print(mat[i][j] + "\t");
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(;
        BufferedReader br = new BufferedReader(in);
        System.out.print("Number of rows: ");
        int rows = Integer.parseInt(br.readLine());
        System.out.print("Number of columns: ");
        int cols = Integer.parseInt(br.readLine());
        Shift obj1 = new Shift(rows, cols);
        Shift obj2 = new Shift(rows, cols);
        System.out.println("ORIGINAL MATRIX:");
        System.out.println("SHUFFLED MATRIX:");

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.