Sei sulla pagina 1di 4

Class Transarray 1/4

/* ashking13th@gmail.com
* javaprogramsbyashking13th.blogspot.in
* ashkingjava.blogspot.in
*
*
* QUESTION
*
* A transpose of an array obtained by interchanging the
* elements of the rows and columns.
* A class Transarray contains a two dimensional integer
* array of order [m*n]. The maximum value possible for
* both 'm' and 'n' is 20.
*
* Design a class Transarray to find the transpose of a
* given matrix . the details of the members are given
* below :
*
* CLASS NAME : Transarray
*
* DATA MEMBERS
* arr[][] : stores th matrix elements
* m : integer to store the number of rows
* n : integer to store the number of columns
*
* MEMBER FUNCTIONS
* Transarray() : default constructor
* Transarray(int mm,int nn) : to initialise the size of matrix,
* m=mm , n=nn
* void fillarray() : to enter the elements of the matrix
* void transpose(transarray A) : to find the transpose of a given matrix
* void disparray() : displays the array in a matrix form
*
* Define main() method to execute the class.
*/
import java.io.*;
public class Transarray
{
int arr[][]=new int[20][20];
/*
* data member to store the matrix
*/
int m;
/*
* data memebr to store number of rows in the matrix
*/
int n;
/*
* data memebr to store number of columns in the matrix
*/
Mar 25, 2014 3:22:33 PM
Class Transarray (continued) 2/4
DataInputStream d=new DataInputStream(System.in);
/*
* input stram object.
* YOUCAN ALSO USE BUFFERED READER
*/
/*
* default constructor to initialise the array
*/
public Transarray()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
arr[i][j]=0;
}
}
}//end of default constructor Transarray()
/*
* parameterised consructor to initialise size of matrix
*/
public Transarray(int mm,int nn)
{
m=mm;
n=nn;
}//end of parameterised constructor
void fillarray()throws IOException
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("enter "+i+"*"+j+" th element");
arr[i][j]=Integer.parseInt(d.readLine());
/*
* inputing elements of the matrix
*/
}
}
}//end of method fillarray()
/*
* method to find the transpose of the inputted array
*/
void transpose(Transarray A)
{
int arr1[][]=new int[20][20];
Mar 25, 2014 3:22:33 PM
Class Transarray (continued) 3/4
/*
* creating a local array to store the
* transpose of the matrix
*/
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
arr1[i][j]=A.arr[j][i];
/*
* storing the element of original array arr with
* poaition i,j at a position j,i in the local array
* i,e., creating the transpose of the matrix.
*/
}
}
System.out.println("transpose");
/*
* printing the transpose array i,e.,arr1 in matrix form
*/
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(arr1[i][j]+"\t");
}
System.out.println();
}
}//end of transpose(transarray A)
/*
* method to display the array (original)
*/
void disparray()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
/*
* main() method to create object and call methods using the object
* to execute the program.
*/
Mar 25, 2014 3:22:33 PM
Class Transarray (continued) 4/4
public void main() throws IOException
{
int mm=0;
int nn=0;
System.out.println("enter no of rows");
mm=Integer.parseInt(d.readLine());
System.out.println("enter no of columns");
nn=Integer.parseInt(d.readLine());
/*
* inputting the number of rows and number of columns from the user
*/
/*
* if and only when no of rows or and columns both are less than 20,
* then only we have to the transpose.
* if any one or both of them exceed 20 we will display the
* appropriate message and not find the transpose, as we have
* defined the matrix with maximum 20 rows and columns only.
*/
if(mm<20&&nn<20)
{
Transarray o1=new Transarray(mm,nn);
/*
* creating object using parameterised constructor
* and then calling functins using it to execute the program.
*/
o1.fillarray();
System.out.println("original");
o1.disparray();
o1.transpose(o1);
}
else
{
System.out.println("no of rows or columns cannot exceed 20");
}
}//end of main
}//end of class
Mar 25, 2014 3:22:34 PM

Potrebbero piacerti anche