Hello! I'm trying to write a program that will eventually carry out matrix multiplication (by another matrix and by a vector) among a few other methods for a homework assignment. However, the assignment says we first need to write a default constructor that creates the 3x3 Identity Matrix. I'm not sure how to write a default constructor. Up until now, I've written this:

public static int[][] create(int size) { 
    int[][] matrix = new int[size][size];
    for(int i = 0; i < size; i++)
      for(int j = 0; j < size; j++)
        matrix[i][j] = (i == j) ? 1 : 0;
    return matrix;

But this just makes an identity matrix when the user puts in the dimension (later). I know that a default constructor is what is used if nothing else specific is written for the program, but how does one write one? Is what I have at all close?

Recommended Answers

All 9 Replies

Also, for this assignment we're not supposed to use the matrix package at all - but seeing as I'm a total beginner, I'm not even sure how I would go about that anyway. I just thought that might be relevant!

Thank you - that did help me understand constructors a bit more. I'll keep those links!
Is it correct to say, then, that if I remove 'static' from what I posted above and let size=3, that will work as the default?

Thank you - that did help me understand constructors a bit more. I'll keep those links!
Is it correct to say, then, that if I remove 'static' from what I posted above and let size=3, that will work as the default?

I see no reason why it wont, the point though of a constructor should be where you declare all variables for the actual methods to accept then call these methods, so i wouldn't suggest putting actual coding in your constructor rather just set the variables needed for the methods to work the correct answers, and then call that method seperately.

public class create {
public static int size=0;
public create(int s1) {
size=s1;
}
    public static int[][] createMatrix() { 
    int[][] matrix = new int[size][size];
    for(int i = 0; i < size; i++)
      for(int j = 0; j < size; j++)
        matrix[i][j] = (i == j) ? 1 : 0;
    return matrix;
}
}

so first call the constructor-create(int s1), then call the method createMatrix() and it should do what you expect, thus you have used a constructor, to prepare your matrix and finally call the method to get your answer.:)

Alright! That makes sense. Thank you very much!

I don't know what you mean here when you say constructor but I see no constructors in your code.

A constructor resides in a class with the same name as the class. The default constructor is a constructor with no input parameters. In my opinion you should write a matrix class, something like this...

public class Matrix {
   // Array that hold the values of the matrix
   private int[][] array;

   // default constructor, instansiates a 3x3 identity matrix
   public Matrix() {
      array = new int[3][3];
      // set array values to 1
   }

   //other constructor, instansiates nxm identity matrix
   public Matrix(int n, int m) {
      array = new int[n][m];
      // set array values to 1
   }

   // method for matrix multiplication
   // Should trow an exception or something if the matrixes are of incompatible dimensions
   public Matrix multiply(Matrix anotherMatrix) throws IncompatibleMatrixException  {
      // return a new matrix that is a multiplication of this matrix by anotherMatrix...
   }

   // More matrix operation methods, for instance methods for setting the values of the matrix.
   public void setValue(int n, int m, int value) {
      array[n][m] = value;
   }

}

Then in your code you can create and operate on matrixes like this...

// Create two 3x3 matrixes.
Matrix a = new Matrix();           // default constructor
Matrix b = new Matrix(3, 3);     // constructor with parameters

// Set some values
a.setValue(1, 2, 73);
b.setValue(0, 1, 66 );

// Multiply the matrixes
Matrix c = a.multiply(b);

Sorry I now see a constructor "create". I would still go for a matrix class.

commented: uhm its solved? -1

cOrRuPtG3n3t!x: Your advice on was so bad that I felt obliged to give some more useful input on how to code in java. Do you always dismiss comments that you feel are hurting your credibility?

cOrRuPtG3n3t!x: Your advice on was so bad that I felt obliged to give some more useful input on how to code in java. Do you always dismiss comments that you feel are hurting your credibility?

@SasseMan: lol, well lets look at the question the OP asked:" However, the assignment says we first need to write a default constructor that creates the 3x3 Identity Matrix. I'm not sure how to write a default constructor." and your ingenius reply is:"I don't know what you mean here when you say constructor but I see no constructors in your code.". and as for hurting my credibility, uhm this is a forum not my job so i couldn't care less about that. Then you say:"Sorry I now see a constructor "create". I would still go for a matrix class.", uhm thats great, but who's constructor are you talking about? because the only constructor posted before yours was mine? and i was not asking a question, and just because you would do it differently doesn't make my answer wrong, i simply just saw you replying to a solved thread, which if it wasn't solved i would have done/said nothing we are all entitled to our opinions. and on that note how is my constructor wrong-just because you used 2 constructors and renamed the class and its constructors-WOW? the java docs i posted would have told the OP that i was just showing him the basics ,i didnt want to do the coding for him, and im sure if yours is better then mine he will use it.

Enjoy your day now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.