
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Menu Driven Program to Perform Matrix Operation
Array in Java is called as a non primitive data type which stores a fixed number of single type values. It is called a one-dimensional array. Whereas matrix refers to a rectangular array or Two-Dimensional array.
In this article, we will see how to perform different matrix operations like addition, subtraction, and multiplication by using Java Menu driven program. We will be implementing the application using a switch case.
To show you some instances
Instance-1
Suppose we have inserted two different matrices of 2 rows and 3 columns. Then we will perform matrix addition and print the result. Let the matrix be: First Matrix: 2 3 5 9 8 7 Second Matrix: 6 4 8 9 5 4 Matrix after addition: 8 7 13 18 13 11
Instance-2
Suppose we have inserted two different matrices of 2 rows and 3 columns. Then we will perform matrix subtraction and print the result. Let the matrix be: First Matrix: 2 3 5 9 8 7 Second Matrix: 6 4 8 9 5 4 Matrix after Subtraction: -4 -1 -3 0 3 3
Instance-3
Suppose we have inserted two different matrices of 2 rows and 3 columns. Then we will perform matrix multiplication and print the result. Let the matrix be: First Matrix: 2 3 5 9 8 7 Second Matrix: 6 4 8 9 5 4 Matrix after Multiplication: 12 12 40 81 40 28
Syntax
To calculate the matrix addition, subtraction and multiplication we use a for loop with some basic logic.
Following is the syntax for “for loop” −
for (statement 1; statement 2; statement 3) { // code block to be executed }
Algorithm
Step-1 − Ask the user to input the two matrices.
Step-2 − Display the menu.
Step-3 − Ask the user to enter their choice.
Step-4 − Use a switch case to go to the choice and perform the operation.
Step-5 − Print the result.
Let’s see the program to understand it clearly.
Example
import java.util.*; public class Main{ public static void main(String args[]){ Scanner s = new Scanner( System.in ); int p, q, m, n; System.out.print("Enter number of rows in first matrix: "); p = s.nextInt(); System.out.print("Enter number of columns in first matrix: "); q = s.nextInt(); System.out.print("Enter number of rows in second matrix: "); m = s.nextInt(); System.out.print("Enter number of columns in second matrix: "); n = s.nextInt(); int a[][] = new int[p][q]; int b[][] = new int[m][n]; int c[][] = new int[m][n]; System.out.println("Enter all the elements of first matrix:"); for (int i = 0; i < p; i++) { for (int j = 0; j < q; j++) { a[i][j] = s.nextInt(); } } System.out.println("Enter all the elements of second matrix:"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { b[i][j] = s.nextInt(); } } System.out.println("First Matrix:"); for (int i = 0; i < p; i++) { for (int j = 0; j < q; j++) { System.out.print(a[i][j]+" "); } System.out.println(""); } System.out.println("Second Matrix:"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(b[i][j]+" "); } System.out.println(""); } mainLoop: while (true) { System.out.println("\n***Menu***"); System.out.println("1. Matrix Addition"); System.out.println("2. Matrix Subtraction"); System.out.println("3. Matrix Multiplication"); System.out.println("4. Terminate the program"); System.out.println("Enter action number (1-4): "); int command; if ( s.hasNextInt() ) { command = s.nextInt(); s.nextLine(); } else { System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER."); s.nextLine(); continue; } switch(command) { case 1: if (p == m &&& q == n){ for (int i = 0; i < p; i++) for (int j = 0; j <n; j++) { for (int k = 0; k < q; k++) { c[i][j] = a[i][j] + b[i][j]; } } } System.out.println("Matrix after addition:"); for (int i = 0; i <p; i++){ for (int j = 0; j < n; j++) { System.out.print(c[i][j]+" "); } System.out.println(""); } } else{ System.out.println("Addition would not be possible"); } break; case 2: if (p == m && q == n){ for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < q; k++) { c[i][j] = a[i][j] - b[i][j]; } } } System.out.println("Matrix after Subtraction:"); for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { System.out.print(c[i][j]+" "); } System.out.println(""); } } else{ System.out.println("Subtraction would not be possible"); } break; case 3: if (p == m && q == n){ for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < q; k++) { c[i][j] = a[i][j] * b[i][j]; } } } System.out.println("Matrix after Multiplication:"); for (int i = 0; i < p; i++){ for (int j = 0; j < n; j++) { System.out.print(c[i][j]+" "); } System.out.println(""); } } else{ System.out.println("Multiplication would not be possible"); } break; case 4: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } }
Output
Enter number of rows in first matrix: 2 Enter number of columns in first matrix: 2 Enter number of rows in second matrix: 2 Enter number of columns in second matrix: 2 Enter all the elements of first matrix: 1 2 3 4 Enter all the elements of second matrix: 5 6 7 8 First Matrix: 1 2 3 4 Second Matrix: 5 6 7 8 ***Menu*** 1. Matrix Addition 2. Matrix Subtraction 3. Matrix Multiplication 4. Terminate the program Enter action number (1-4): 1 Matrix after addition: 6 8 10 12 ***Menu*** 1. Matrix Addition 2. Matrix Subtraction 3. Matrix Multiplication 4. Terminate the program Enter action number (1-4): 2 Matrix after Subtraction : -4 -4 -4 -4 ***Menu*** 1. Matrix Addition 2. Matrix Subtraction 3. Matrix Multiplication 4. Terminate the program Enter action number (1-4): 3 Matrix after Multiplication: 5 12 21 32 ***Menu*** 1. Matrix Addition 2. Matrix Subtraction 3. Matrix Multiplication 4. Terminate the program Enter action number (1-4): 4 Program terminated
In this article, we explored how to perform matrix operations in Java by using a menu driven approach.
- Related Articles
- Java Menu Driven Program to Perform Array Operation
- Java Menu Driven Program to Perform Queue Operation
- Java Menu Driven Program to Perform Basic String Operations
- Java Menu Driven Program to Check Type of a Number
- Java Menu Driven Program to Determine Cost of New Membership
- Java Program to perform XOR operation on BigInteger
- Java Program to perform AND operation on BigInteger
- Java Menu Driven Program to Check Positive Negative or Odd Even Number
- JAVA Menu Driven Program to Check Character is String, Number or Special Character
- Menu Driven C++ Program for a Simple Calculator
- Program to perform excel spreadsheet operation in Python?
- C++ Program to Perform Matrix Multiplication
- C++ Program to Perform Addition Operation Using Bitwise Operators
- C program to perform union operation on two arrays
- C program to perform intersection operation on two arrays
