Quick Newbie Java Question

Associate
Joined
2 Nov 2007
Posts
488
Hello,

Im trying to go through some basic Java exercises on arrays, and was wondering if you could give me a point in the right direction.

The question is:
# Add a constructor Matrix(double[][] m) that can store as a 2-D array a matrix with an arbitrary number of rows and columns. The values of the individual elements are of type double.
# Add the methods nCols(), nRows() returning (as an int) the number of columns and rows.

So far i have:
Code:
	// Empty constructor
	public Matrix(double[][] m){}
	
	public static int nCols(Matrix m){
		return m[0].length;
	}
	
	public static int nRows(Matrix m){
		return m.length;
	}

Which Eclipse doesnt like. I think its becuase i havent set the constructor correctly (or at all!) and was wondering if you could give me any help? I uderstand arrays in general, but i dont understand how i am meant to use them as a constructor.

Cheers
 
Last edited:
You've declared a constructor there, but you haven't defined a class (i.e. Matrix). From only the code you've supplied, you have only declared a function called Matrix that accepts a multidimensional array.

You rnCols and nRows are expected a parameter of type Matrix, which so far has not yet been defined.

I've just implemented it myself and it works, you just need to define the class, and probably add a 'getter' method to access the stored double array. Currently when you have "m." it is not referencing the array. E.g. I've got something like this in my nRows function: "m.getMatrix().length;".

Does that help?
 
Last edited:
Thanks for the reply.

Well here is a copy of the full code: http://pastebin.com/pS1dYnid (the main class being called Matrix if that helps?)

What would my getter function actually be trying to retrieve?

The main thing thats confusing me with the constructor (from the wording of the exercise question) is that it needs to be Matrix (by name) but array (by function), if that makes sense? So eclipse is complaining "The type of the expression must be an array type but it resolved to Matrix"

Im going to have another read back over constructors...

Sorry for being so dim!
 
Code:
public class Matrix {

        // instance variable for storage of matrix
	double[][] m_matrix;
	
        // Constructor stores the argument into the instance variable variable
	public Matrix(double[][] m)
	{
		matrix = m;
	}
		
	public static int nCols(Matrix m)
	{
                // Accessing m's instance variable directly
		return m.matrix[0].length;
	}
	
	public static int nRows(Matrix m)
	{
                // Accessing m's instance variable directly
		return m.matrix.length;
	}
}

I've taken out the 'getter' as at this stage it would only complicate things.

Eclipse is showing you a problem inside your nRows and nCols because what you were attempting to access from Matrix doesn't exist. You were trying to access the Matrix object as if it were an array itself, but it isn't, it is a type you have defined that currently has no attributes or behaviour.

So you are almost there, and referring to the above I have modified the constructor to store the multidimensional array inside an instance variable. You can then access that instance variable using the 'dot' as you can see in nRows and nCols.

Does that help?
 
Hey BlackDragon, thanks for your help - i really appreciate it.

I understand what youve said, and my solution is very similar except my methods are non static. I just have the problem whereby i can know what i want in my head, and if i see the code in front of me i can understand it - its just getting every detail correct when i try to code from scratch. Hopefully this will improve with practise.

Sorry for not getting back sooner, but ive been trying to wade through some exercises and i think ive just about managed it. Im going to be attempting some exam style practise questions tonight / tommorow, so will almosy definately be back to ask some more questions!

Thanks again
 
No problem :)

Programming will take time and experience to learn, so don't be put off when you are struggling a bit. Eventually you will visualize the answer like it was second nature to you :)
 
Back
Top Bottom