ISC Practicals and Theory - Matrix Rotation 180/270 (OOP)
Updated 21 Oct 2026
This version of the matrix rotation program is built around a class. A parameterised constructor receives the order and the angle and creates the arrays, and separate methods rotate180() and rotate270() each do one rotation. The program reads an n x n matrix and an angle of 180 or 270 degrees, prints the original matrix and the rotated one.
Definition
Rotation by 180 degrees is the same clockwise or anticlockwise: the matrix is turned upside down. Rotation by 270 degrees clockwise is the same as 90 degrees anticlockwise. For order $n$, with original $a$ and rotated $r$:
- 180 degrees:
r[i][j] = a[n-1-i][n-1-j] - 270 degrees clockwise:
r[i][j] = a[j][n-1-i]
For the rows 1 2 and 3 4, 180 degrees gives 4 3 and 2 1, and 270 degrees gives 2 4 and 1 3. An order outside 2 to 10, or an angle other than 180 or 270, is invalid.
Algorithm
- In
main, read the order intosize. If it is less than 2 or more than 10, printInvalid sizeand stop. - Read the angle into
angle. If it is neither 180 nor 270, printInvalid choiceand stop. - Create the object with
new MatrixRotation(size, angle). The constructor storesnandchand creates the arraysaandrof sizen x n. - Call
readMatrix()to read the elements intoa. - If
angleis 180 callrotate180(), otherwise callrotate270(). Both fillrfromawith two nested loops overiandj;ais never changed. - Print
Original matrix:and calldisplay(a), then print the heading with the angle and calldisplay(r).
Program
The constructor sets up the data members, readMatrix() reads the elements, rotate180() and rotate270() build the rotated matrix, and display() prints a matrix.
import java.util.Scanner;
/**
* Class MatrixRotation (OOP version) stores an n x n matrix and rotates it
* by 180 or 270 degrees into a second matrix.
* 180 degrees is the same clockwise or anticlockwise;
* 270 degrees clockwise is the same as 90 degrees anticlockwise.
*/
public class MatrixRotation
{
int a[][]; // stores the original matrix
int r[][]; // stores the rotated matrix
int n; // order of the square matrix (rows = columns = n)
int ch; // choice of rotation: 180 or 270 degrees
/**
* Parameterised constructor.
* nn is the order of the matrix, cc the angle (180 or 270).
* Stores them and creates the two n x n arrays.
*/
MatrixRotation(int nn, int cc)
{
n = nn;
ch = cc;
a = new int[n][n];
r = new int[n][n];
}
/**
* Reads the n x n elements into a[][].
*/
void readMatrix(Scanner sc)
{
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < n; i++) // i runs over the rows
{
for (int j = 0; j < n; j++) // j runs over the columns
{
a[i][j] = sc.nextInt();
}
}
}
/**
* Rotates by 180 degrees: the element at row i, column j
* comes from row n-1-i, column n-1-j of the original.
*/
void rotate180()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
r[i][j] = a[n - 1 - i][n - 1 - j];
}
}
}
/**
* Rotates by 270 degrees clockwise (90 degrees anticlockwise):
* the element at row i, column j comes from row j, column n-1-i.
*/
void rotate270()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
r[i][j] = a[j][n - 1 - i];
}
}
}
/**
* Prints any n x n matrix x[][] row by row.
*/
void display(int x[][])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(x[i][j] + "\t"); // tab between elements
}
System.out.println(); // new line after each row
}
}
/**
* main reads and checks n and the angle, creates the object
* and calls the methods in order.
*/
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the order n of the matrix (2 to 10): ");
int size = sc.nextInt();
if (size < 2 || size > 10) // size out of range
{
System.out.println("Invalid size");
}
else
{
System.out.print("Enter the angle of rotation (180 or 270): ");
int angle = sc.nextInt();
if (angle != 180 && angle != 270) // only these two angles are allowed
{
System.out.println("Invalid choice");
}
else
{
MatrixRotation obj = new MatrixRotation(size, angle);
obj.readMatrix(sc);
if (angle == 180)
obj.rotate180();
else
obj.rotate270();
System.out.println("Original matrix:");
obj.display(obj.a);
System.out.println("Matrix rotated by " + angle + " degrees:");
obj.display(obj.r);
}
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
| a | int[][] | Data member; stores the original matrix |
| r | int[][] | Data member; stores the rotated matrix |
| n | int | Data member; order of the square matrix |
| ch | int | Data member; angle of rotation, 180 or 270 |
| nn | int | Constructor parameter; order passed from main |
| cc | int | Constructor parameter; angle passed from main |
| sc | Scanner | Reads the order, the angle and the elements (in main and as a parameter of readMatrix()) |
| x | int[][] | Parameter of display(); the matrix to be printed |
| i | int | Loop counter for the rows |
| j | int | Loop counter for the columns |
| args | String[] | Command line arguments of main; not used |
| size | int | Order of the matrix read in main |
| angle | int | Angle read in main |
| obj | MatrixRotation | Object created in main that calls the methods |
Sample input and output
Sample 1
Enter the order n of the matrix (2 to 10): 3
Enter the angle of rotation (180 or 270): 270
Enter the elements of the matrix:
1 2 3
4 5 6
7 8 9
Original matrix:
1 2 3
4 5 6
7 8 9
Matrix rotated by 270 degrees:
3 6 9
2 5 8
1 4 7 Sample 2
Enter the order n of the matrix (2 to 10): 2
Enter the angle of rotation (180 or 270): 180
Enter the elements of the matrix:
-1 5
0 -1
Original matrix:
-1 5
0 -1
Matrix rotated by 180 degrees:
-1 0
5 -1 Sample 3
Enter the order n of the matrix (2 to 10): 3
Enter the angle of rotation (180 or 270): 90
Invalid choiceSample 4
Enter the order n of the matrix (2 to 10): 11
Invalid sizeDry run
Take $n = 2$, angle = 180 and the matrix with rows -1 5 and 0 -1. rotate180() uses r[i][j] = a[1-i][1-j].
| i | j | Element used | r[i][j] |
|---|---|---|---|
| 0 | 0 | a[1][1] | -1 |
| 0 | 1 | a[1][0] | 0 |
| 1 | 0 | a[0][1] | 5 |
| 1 | 1 | a[0][0] | -1 |
The rotated matrix has rows -1 0 and 5 -1, the same as the second sample run.
Exam tip: Check the order and the angle in main before creating the object, so the constructor only ever receives valid values.
Variations
- Rotation by 90 degrees "Rotate a square matrix by 90 degrees clockwise and display the original and the rotated matrix."
- Anticlockwise rotation "Rotate a square matrix by 90 degrees anticlockwise and display it."
- Rotation by any multiple of 90 "Accept an angle that is a multiple of 90 and rotate the matrix accordingly."