Class Transarray
1/4
/*
[email protected] * javaprogramsb javaprogramsbyashking13th yashking13th.blogspot.in .blogspot.in * ashkingjava.b ashkingjava.blogspot.in logspot.in * * * QUESTION * * A transpose of an array obtained by interchanging 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(i Transarray(int nt mm,int nn) : to initialise the size of matrix, * m=mm , n=nn * void fillarray() : to enter the elements of the matrix * void transpose(tra transpose(transarray nsarray A) : to find the transpose of a given matrix * void disparray( disparray() ) : displays the array in a matrix form * * Define main() method to execute the class. */
import java.io.*; import java.io.*; public class class Transarray Transarray { int arr[][]= int arr[][]=new new int int[20][20]; [20][20]; /* * data member to store the matrix */ int m; int m; /* * data memebr to store number of rows in the matrix */ int n; 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 DataInputStream d=new d=new DataInputStream(System. DataInputStream(System.in); in); /* * input stram object. * YOUCAN ALSO USE BUFFERED READER */
/* * default constructor to initialise the array */ public Transarray() public Transarray() { for( for (int i=0;i<20;i++) i=0;i<20;i++) { for( for (int j=0;j<20;j++) j=0;j<20;j++) { arr[i][j]=0; } } }//end of default constructor Transarray()
/* * parameterise parameterised d consructor to initialise size of matrix */ public Transarray( public Transarray(int int mm, mm,int int nn) nn) { m=mm; n=nn; }//end of parameterise parameterised d constructor void fillarray()throws IOException void fillarray()throws { for( for (int int i=0;i
/* * method to find the transpose of the inputted array */ void transpose(Transarray void transpose(Transarray A) { int arr1[][]= int arr1[][]=new new int int[20][20]; [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( for (int i=0;i<20;i++) i=0;i<20;i++) { for( for (int j=0;j<20;j++) 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" System.out.println( "transpose"); ); /* * printing the transpose array i,e.,arr1 in matrix form */ for( for (int int i=0;i
/* * method to display the array (original) */ void disparray() void disparray() { for( for (int int i=0;i
/* * 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 void main() main() throws IOException { int mm=0; int mm=0; int nn=0; int nn=0; System.out.println("enter no of rows"); System.out.println("enter rows"); mm=Integer.parseInt(d.readLine()); System.out.println("enter no of columns"); System.out.println("enter 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) if (mm<20&&nn<20) { Transarray o1=new o1=new Transarray(mm,nn); /* * creating object using parameteris parameterised ed constructor * and then calling functins using it to execute the program. */ o1.fillarray(); System.out.println("original"); System.out.println("original" ); o1.disparray(); o1.transpose(o1);
} else {
System.out.println("no of rows or columns cannot exceed 20"); System.out.println("no 20"); } }//end of main
}//end of class
Mar 25, 2014 3:22:34 PM