dawnoflife
dawnoflife

Reputation: 1652

matrix manipulation in java

Hey! I have matrix class and i want to add, multiply and take transpose of the matrix. for the structure of the code...I made a matrix class with a constructor, basic get methods and the methods to do the operations. Here's what i have. i'm not sure if i need a constructor or not. I will have an application class that will call the main method. i want to know if so far I'm good with the thought process?

import java.io.*;
import java.util.*;
import java.util.Scanner;

class Matrix {
    double[][] element;
    int row, col;

    Matrix(int a, int b) {
        row = a;
        col = b;
        element = new double[a][b];
    }

    public double getElement(int a, int b) {
        return element[a][b];
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }
}

Upvotes: 1

Views: 3055

Answers (3)

jmq
jmq

Reputation: 10370

  1. Your getRow and getCol functions should have better names, as they read like you're retrieving specific row/columns from your matrix (when you're really retrieving their sizes of the row/col).
  2. Add a "toString" function for pretty printing the internal contents of the matrix.
  3. Get rid of the a/b stuff and give them row/col names instead. It makes the code more readable and easier to follow.

    Matrix(int row, int col) {
    this.row = row;
    this.col = col;

    element = new double[row][col];
    }

Same with the getElement() function. It's difficult to tell what the parameters are to the function (which one is the row, which is the column?). You have to read through the class to figure out which is which.

public double getElement(int row, int col) {
    return element[row][col];
}

Upvotes: 1

Owen
Owen

Reputation: 1551

Looks fairly ok so far (although the matrix is declared twice, as the commentor noted). It will definitely be useful to have a constructor in order to instantiate your internal matrix, otherwise you'll (rather pointlessly) have to do it with another method, and you'd risk calling methods that try to access an uninstantiated matrix (which would not be useful).

Regarding the two matrix declarations: it's like any other variable. You declare with double[][] matrixname, then instantiate with matrixname = new double[a][b] (or do both at once if useful). If you do double matrixname = new double[a][b] inside the constructor, you'll be creating a local variable there, which is then lost when the constructor code ends.

Also, it might be good to use slightly better method names for clarity. getRow and getCol suggest, to me, that you'd be returning whole rows and whole columns, not the array bounds. Maybe think in terms of width and height?

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 992857

Yes, you will need a constructor for your class. The constructor you have there is a good start, but won't quite compile at the moment. In particular, the initialisation of the element array needs some work.

The purpose of the constructor is to set up the data members of the object into a form that is suitable for further work. In your case, it certainly makes sense to save the height and width of the matrix, and set up the arrays of elements.

Upvotes: 0

Related Questions