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:
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!
 
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
 
Back
Top Bottom