# Java program to do Matrix operation

• February 1st, 2009, 05:11 PM
Java program to do Matrix operation
I have an assignment that states:
2. When you are adding two matrices together, you add similar elements together and place them in a new matrix. Because of this, you need to have matrices with identical sizes. Create a Matrix class, with methods such as add(Matrix a), subtract(Matrix a) and multiplyScalar(double n).

I have no idea where to start, would anyone be able to help:?
• February 2nd, 2009, 05:46 AM
JavaPF
Hello saladfingers73 and welcome to the Java Programming Forums :D

Do you know anything about Matrix? I suggest you take a look at:

Getting and Setting Arrays and Their Components (The Java™ Tutorials > The Reflection API > Arrays and Enumerated Types)

Here is an example of a Matrix:

Code :

```class MatrixExample{   public static void main(String[] args) {   int array[][]= {{1,3,5},{2,4,6}}; System.out.println("Row size= " + array.length); System.out.println("Column size = " + array[1].length); outputArray(array); }   public static void outputArray(int[][] array) {   int rowSize = array.length; int columnSize = array[0].length; for(int i = 0; i <= 1; i++) { System.out.print("["); for(int j = 0; j <= 2; j++) { System.out.print(" " + array[i][j]); } System.out.println(" ]"); } System.out.println(); } }```
See if this helps you and post back if you need help..
• March 5th, 2012, 11:09 AM
santoshbharat
/*
A Program to add the two matrices
*/
package com.bharat;
import java.util.*;
public class MyNewArrayConcepts {
public static void main(String[] ar){
//creating two dimensional arrays
int[][] arr=new int[3][3];
int[][] arr1=new int[3][3];
Scanner sc=new Scanner(System.in);
System.out.println("Enter elements of first array");
//accepting the elements for first array by taking the help of scanner object present in java.util
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=sc.nextInt();
}
}
//accepting the elements for second array by taking the help of scanner object present in java.util
System.out.println("Enter elements of Second array");
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr1[i].length;j++){
arr1[i][j]=sc.nextInt();
}
}
//Displaying the elements of first array in the matrix form(3*3)
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" \t");
}
System.out.println();
}
System.out.println();
//Displaying the elements of Second array in the matrix form(3*3)
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr1[i].length;j++){
System.out.print(arr1[i][j]+" \t");

}
System.out.println();
}
//Adding the two matrices and Displaying the result of two matrices
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+arr1[i][j]+" \t");
}
System.out.println();
}
}
}
• March 5th, 2012, 12:58 PM
copeg
@santoshbharat, why are you resurrecting a post that is over 3 years old. Do you have a question? Is the code you posted meant to spoonfeed a solution?
• March 5th, 2012, 10:55 PM
santoshbharat
Quote:

Originally Posted by copeg
@santoshbharat, why are you resurrecting a post that is over 3 years old. Do you have a question? Is the code you posted meant to spoonfeed a solution?

Hey copeg i am sorry man,i am just learning java and figured like posting what i get to write about programming.i know its amateur way of writing.Could you please redesign the code to look better.Thanks in advance
• March 7th, 2012, 08:17 AM
arvindbis