
- 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
Replace the Matrix Elements by Its Square in Java?
Matrices are nothing but it’s more than a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.
As per the problem statement the task is replace the matrix elements by its square.
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose the original matrix is {{8,3,2}, {12,7,9}, {6,4,10}};
After replacing the matrix element by its square, the result matrix will be
64 9 4 144 49 81 36 16 100
Instance-2
Suppose the original matrix is {{4,3,7}, {2,3,9}, {3,4,9}};
After replacing the matrix element by its square, the result matrix will be
16 9 49 4 9 81 9 16 81
Instance-3
Suppose the original matrix is {{1,2,3}, {2,1,3}, {3,2,1}};
After replacing the matrix element by its square, the result matrix will be
1 4 9 4 1 9 9 4 1
Algorithm
Step 1 − Initialise and declare the matrix.
Step 2 − Create another matrix to store the square value.
Step 3 − Multiply the matrix element by itself or use inbuilt pow() function to get the square.
Step 4 − Print the result.
Syntax
To get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.
Following is the syntax to get power of 2 by using the method-
double power = math.pow (inputValue,2)
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix with pow() Function.
By Using User Defined Method without Inbuilt Function.
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Matrix with pow() Function
In this approach, matrix elements will be initialized in the program. Then as per the algorithm replace the matrix elements by its square. Here we will make use of inbuilt Pow() function to get the square of an element.
Example
public class Main { //main method public static void main(String args[]){ //Initialising the matrix int a[][]={{8,3,2},{12,7,9},{6,4,10}}; //creating another matrix to store the square value int c[][]=new int[3][3]; //Multiplying the matrix element by itself to get the square for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ int element = a[i][j]; c[i][j]+= Math.pow(element,2); //printing the result System.out.print(c[i][j]+" "); }//end of j loop //new line System.out.println(); }//end of i loop } }
Output
64 9 4 144 49 81 36 16 100
Approach-2: By Using User Defined Method without Inbuilt Function
In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm replace the matrix elements by its square. Here, we will multiply the element with itself to get the square of an element.
Example
public class Main { //main method public static void main(String args[]){ //Initialising the matrix int a[][]={{4,3,7},{2,3,9},{3,4,9}}; //calling user defined method square(a); } //user defined method static void square(int a[][]) { //creating another matrix to store the square value int c[][]=new int[3][3]; //Multiplying the matrix element by itself to get the square for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ c[i][j]+=a[i][j] * a[i][j]; //printing the result System.out.print(c[i][j]+" "); }//end of j loop //new line System.out.println(); }//end of i loop } }
Output
16 9 49 4 9 81 9 16 81
In this article, we explored different approaches to replacing matrix elements by its square by using Java programming language.
- Related Articles
- Replace the Array Elements by Its Corresponding Rank in Java
- Replace Negative and Positive Matrix Elements with 0 & 1 in Java
- How to find the mean of a square matrix elements by excluding diagonal elements in R?
- How to Decrement Matrix Elements by One in Java?
- Spiraling the elements of a square matrix JavaScript
- Replace Odd Numbers with Square root & Even Numbers with Square in Java
- Python – Sort Matrix by Number of elements greater than its previous element
- Java Program to Replace Elements in a List
- Finding the maximum square sub-matrix with all equal elements in C++
- Replace all the elements of a Vector with Collections.fill() in Java
- Java Program to Rotate Matrix Elements
- How to Check if the Matrix is a Magic Square in Java?
- Row-wise common elements in two diagonals of a square matrix in C++
- Python Program to Remove First Diagonal Elements from a Square Matrix
- Python program to replace first ‘K’ elements by ‘N’
