I wrote this matrix addition program and I dont know why but I keep getting this two errors on lines 61 and 63 and i dont want to handle the exceptions but just throwing would do
error: unreported exception IOException; must be caught or declared
The code of the program is as follows:
import java.io.*;
class Arr
{
int r,c;
int arr[][];
Arr(int r,int c)
{
this.r=r;
this.c=c;
arr=new int[r][c];
}
int[][] getMatrix()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("enter element");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
return arr;
}
int[][] findsum(int a[][],int b[][])
{
int temp[][]=new int[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
temp[i][j]=a[i][j]+b[i][j];
return temp;
}
void putmatrix(int res[][])
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println(res[i][j]+"\t");
}
}
System.out.println();
}
}
class Matrixsum
{
public static void main(String args[])
{
Arr obj1=new Arr(3,3);
Arr obj2=new Arr(3,3);
int x[][],y[][],z[][];
System.out.println("\nEnter matrix 1");
x=obj1.getMatrix();
System.out.println("Enter matrix 2");
y=obj2.getMatrix();
z=obj1.findsum(x,y);
System.out.println("the sum matrix is");
obj2.putmatrix(z);
}
}
The design of class
Arris a bit strange. Instead of using two redundant objectsobj1andobj2, you should condense it to one object, say one called arrProcessor. So the calls would look like:Even better, instead of using arrays, you should wrap the arrays in objects, say in a class called Matrix:
So now the code would look like: