You are on page 1of 5

Class matrix_operation

1/3

package Practice_Question.class_object_constructor;
/**
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/

QUESTION
A class matrix_operation is declared whose details are given below:
CLASS NAME

: matrix_operation

DATA MEMBERS
arr[][] :a double dimentional array of integers of size 10*10
m,n
:integers to store actual size of rows and columns
x
:integer to store a number
MEMBER METHODS
void getrowcolumn() :to input values of m and n as rows and columns
void get matrix()
:to enter a matrix arr[][] of m*n
void print_mat_and-sum(0 :to print the matrix of m*n and also
print sum of greatest element from each row
void change_diagonal
:enter the value of x.Change the values of
both the diagonals of matrix arr[][] to x,
if m=n and print the new matrix, otherwise
print the message
"IT IS NOT A SQUARE MATRIX, SO CHANGE IS NOT POSSIBLE".
SAMPLE DATA
INPUT
3
1
2
4

5
6
4
3

7
4
7
2

4
9
5
8

value of x=9
OUTPUT
RESULTANT MATRIX
9
1
2
9

5
9
9
3

7
9
9
2

9
9
5
9

Specify the class matrix_operation by giving details of all functions.


Write a main() function to find the solution of the above problem.

import java.io.*;

Jan 10, 2015 10:11:27 PM

Class matrix_operation (continued)

2/3

public class matrix_operation


{
/*
* declaring data members
*/
int arr[][]=new int[10][10];
int m,n,x;
DataInputStream d=new DataInputStream(System.in);
/*
* method to input number of rowsand columns of the matrix
*/
void getrowcolumn()throws IOException
{
System.out.println("ENTER NO. OF ROWS");
m=Integer.parseInt(d.readLine());
System.out.println("ENTER NO. OF COLUMNS");
n=Integer.parseInt(d.readLine());
}
/*
* method to input the matrix
*/
void getmatrix()throws IOException
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("ENTER ELEMENT OF ROW "+i+" AND COLUMN
"+j);
arr[i][j]=Integer.parseInt(d.readLine());
}
}
}
/*
* method to find sum of greatest element of each row
*/
void print_mat_and_sum()
{
int t=0;
int s=0;
int a[]=new int[m];
/*
* printing matrix
*/
for(int i=0;i<m;i++)
{
a[i]=arr[i][0];
for(int j=0;j<n;j++)
Jan 10, 2015 10:11:27 PM

Class matrix_operation (continued)

3/3

{
System.out.print(arr[i][j]+"\t");
//finding largest number of a row
if(a[i]<arr[i][j])
{
a[i]=arr[i][j];
}
}
//finding sum of largest number of each row
s=s+a[i];
System.out.println();
}
System.out.println("SUM OF LARGEST NUMBERS OF EACH ROW IS \t"+s);
}
void change_diagonal()throws IOException
{
int x=0;
if(m==n)//checking for a square matrix
{
int z=m-1;
System.out.println("ENTER NEW DIAGONAL VALUE");
x=Integer.parseInt(d.readLine());
//inputing new diagonal value
System.out.println();
System.out.println();
for(int i=0;i<m;i++)
{
arr[i][i]=x;
arr[i][z-i]=x;

//updating left diagonal


//updating right diagonal

}
}
else
{
System.out.println("IT IS NOT A SQUARE MATRIX ,SO CHANGE IS NOT PO
SSIBLE");
}
}
public void main() throws IOException
{
getrowcolumn();
getmatrix();
print_mat_and_sum();
change_diagonal();
print_mat_and_sum();
}//end of main
}//end of class

Jan 10, 2015 10:11:27 PM

matrix_o1.main();
ENTER NO. OF ROWS
5
ENTER NO. OF COLUMNS
5
ENTER ELEMENT OF ROW 0 AND COLUMN 0
1
ENTER ELEMENT OF ROW 0 AND COLUMN 1
2
ENTER ELEMENT OF ROW 0 AND COLUMN 2
3
ENTER ELEMENT OF ROW 0 AND COLUMN 3
4
ENTER ELEMENT OF ROW 0 AND COLUMN 4
5
ENTER ELEMENT OF ROW 1 AND COLUMN 0
2
ENTER ELEMENT OF ROW 1 AND COLUMN 1
3
ENTER ELEMENT OF ROW 1 AND COLUMN 2
4
ENTER ELEMENT OF ROW 1 AND COLUMN 3
5
ENTER ELEMENT OF ROW 1 AND COLUMN 4
6
ENTER ELEMENT OF ROW 2 AND COLUMN 0
3
ENTER ELEMENT OF ROW 2 AND COLUMN 1
4
ENTER ELEMENT OF ROW 2 AND COLUMN 2
5
ENTER ELEMENT OF ROW 2 AND COLUMN 3
6
ENTER ELEMENT OF ROW 2 AND COLUMN 4
7
ENTER ELEMENT OF ROW 3 AND COLUMN 0
3
ENTER ELEMENT OF ROW 3 AND COLUMN 1
4

ENTER ELEMENT OF ROW 3 AND COLUMN 2


5
ENTER ELEMENT OF ROW 3 AND COLUMN 3
6
ENTER ELEMENT OF ROW 3 AND COLUMN 4
7
ENTER ELEMENT OF ROW 4 AND COLUMN 0
4
ENTER ELEMENT OF ROW 4 AND COLUMN 1
5
ENTER ELEMENT OF ROW 4 AND COLUMN 2
6
ENTER ELEMENT OF ROW 4 AND COLUMN 3
7
ENTER ELEMENT OF ROW 4 AND COLUMN 4
8
1

SUM OF LARGEST NUMBERS OF EACH ROW IS

33

ENTER NEW DIAGONAL VALUE


13

13

13

13

13

13

13

13

13

13

SUM OF LARGEST NUMBERS OF EACH ROW IS

65

You might also like