Transpose of a Matrix in Java

A transpose of an array is obtained by interchanging the elements of rows and columns.

A class TransArray contains a two dimensional integer array of order [ m x 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 of the class are given below:
Class name: TransArray
Data members:
arr[][]: stores the 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 initialize the size of the matrix, m = mm, n = nn.
void fillArray(): to enter elements into the matrix.
void transpose(TransArray a): to find the transpose of a given matrix.
void dispArray(): displays the array in a matrix form.

Specify the class TransArray giving details of the constructor, void fillArray(), void transpose(TransArray) and void dispArray().

Write the main() function also to create an object of the class and enable the task accordingly.

import java.io.*;
class TransArray{
    int arr[][];
    int m;
    int n;
    public TransArray(){
        arr = null;
        m = 0;
        n = 0;
    }
    public TransArray(int mm, int nn){
        m = mm;
        n = nn;
        if(m > 20)
            m = 20;
        if(n > 20)
            n = 20;
        arr = new int[m][n];
    }
    public void fillArray()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter matrix elements:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                arr[i][j] = Integer.parseInt(br.readLine());
            }
        }
    }
    public void transpose(TransArray a){
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                this.arr[i][j] = a.arr[j][i];
            }
        }
    }
    public 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();
        }
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Number of rows: ");
        int rows = Integer.parseInt(br.readLine());
        System.out.print("Number of columns: ");
        int cols = Integer.parseInt(br.readLine());
        TransArray obj1 = new TransArray(rows, cols);
        TransArray obj2 = new TransArray(cols, rows);
        obj1.fillArray();
        System.out.println("Original Matrix:");
        obj1.dispArray();
        obj2.transpose(obj1);
        System.out.println("Transposed Matrix:");
        obj2.dispArray();
    }
}

3 thoughts on “Transpose of a Matrix in Java

  1. Sir,2 programs is given which states that:
    1) A financial Institution has only two kinds of accounts,Simple & Compound.

    The class Account has two protected attributes accountNumber (+ve int) and principal ( a double precision real number to store the money deposited initially).The interest for a Simple account is calculated by using the simple interest formula:

    SI=PRT/100

    The interest for a Compound account is calculated by using the compound interest formula:

    CI=P[1+/100]^T-P

    Rate & Time for two kinds of account may vary.

    Model the above situation by defining classes Account,Simple & Compound where Simple & Compound are derived classes from class Account.

    Write appropriate constructors for passing various attributes for the three classes.
    Write the functions double interest() that calculate appropriate interest and return interest for the class Simple & Compound.

    Also write the functions display() for the three classes which printout all the attribute values as

    account number=……………..
    principal=……………….
    rate=………………
    time=………………
    interest=………………

    with each attribute on a single line.Make sure that each display() function is correctly placed in the appropriate class.NO NEED of writing Main().

    2) A class Personal contains employee details and another class Retire calculates the employee’s Provident Fund and Gratuity.

    The details of the 2 classes are given below:

    Class Name : Personal
    Name : stores the employee name
    Pan : stores the employee pan number
    basic pay : stores the employee basic salary(in decimals)
    acc no : stores the employee bank account number

    Member Functions :
    Personal(…) : parameterized constructor to assign value to data members
    void display() : to display the employee details

    Class Name : Retire
    Yrs : stores the employee years of service
    Pf : stores the employee provident fund amount(in decimals)
    Grat : stores the employee gratuity amount(in decimals)

    Member Functions :
    Retire(…) : parameterized constructor to assign value to data members of
    both the classes
    void provident() : calculates the PF as (2% of the basic pay) * years of service
    void gratuity() : calculates the gratuity as 12 months salary,if the years of service
    is more than or equal to 10 years else the gratuity amount is nil.
    void display1() : Displays the employee details along with the PF(Provident Fund)
    and gratuity amount.

    Specify the class Personal giving details of the constructor and member functions
    void display().Using the concept of Inheritance,specify the class Retire giving details of a constructor,and the member functions void provident(),void gratuity() and the void display1().The main() NEED NOT be written.

  2. A class Personal contains employee details and another class Retire calculates the employee’s Provident Fund and Gratuity.

    The details of the 2 classes are given below:

    Class Name : Personal
    Name : stores the employee name
    Pan : stores the employee pan number
    basic pay : stores the employee basic salary(in decimals)
    acc no : stores the employee bank account number

    Member Functions :
    Personal(…) : parameterized constructor to assign value to data members
    void display() : to display the employee details

    Class Name : Retire
    Yrs : stores the employee years of service
    Pf : stores the employee provident fund amount(in decimals)
    Grat : stores the employee gratuity amount(in decimals)

    Member Functions :
    Retire(…) : parameterized constructor to assign value to data members of
    both the classes
    void provident() : calculates the PF as (2% of the basic pay) * years of service
    void gratuity() : calculates the gratuity as 12 months salary,if the years of service
    is more than or equal to 10 years else the gratuity amount is nil.
    void display1() : Displays the employee details along with the PF(Provident Fund)
    and gratuity amount.

    Specify the class Personal giving details of the constructor and member functions
    void display().Using the concept of Inheritance,specify the class Retire giving details of a constructor,and the member functions void provident(),void gratuity() and the void display1().The main() NEED NOT be written.

  3. Sir can you please do this program?

    A financial Institution has only two kinds of accounts,Simple & Compound.

    The class Account has two protected attributes accountNumber (+ve int) and principal ( a double precision real number to store the money deposited initially).The interest for a Simple account is calculated by using the simple interest formula:

    SI=PRT/100

    The interest for a Compound account is calculated by using the compound interest formula:

    CI=P[1+/100]^T-P

    Rate & Time for two kinds of account may vary.

    Model the above situation by defining classes Account,Simple & Compound where Simple & Compound are derived classes from class Account.

    Write appropriate constructors for passing various attributes for the three classes.
    Write the functions double interest() that calculate appropriate interest and return interest for the class Simple & Compound.

    Also write the functions display() for the three classes which printout all the attribute values as

    account number=……………..
    principal=……………….
    rate=………………
    time=………………
    interest=………………

    with each attribute on a single line.Make sure that each display() function is correctly placed in the appropriate class.NO NEED of writing Main().

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.