Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Parameters between classes

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say I have a class file with an algorithm which puts values into a matrix like structure using two arrays. How do i then pass these values into another class which can use them for further calculations???

I hope thats enough information. Cant really think of anything else i can tell you about it.

Cheers
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you a class which represent your "matrix like structure" ?
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah. Ive created the matrix like structure. I have calculated the values that need to go into it. I just need to pass each of the variables into another class file so that I can use each of the values in an algorithm.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you could pass an instance of you matrix as a parameter to the second class method : If your Matrix class has accessors for its memebers, I don't see any problem.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any web links you could give me that would show me how to do that?
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for that. Is that the ONLY way to pass parameters from a matrix like structure into a program that will use the values in an algorithm?

The first class is just setting up the matrix? What are the other 2 methods doing?
[ October 24, 2005: Message edited by: Sam Bluesman ]
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for that. Is that the ONLY way to pass parameters from a matrix like structure into a program that will use the values in an algorithm?

Certainly not...
You can pass each component of the object as different parameters too, ...

The first class is just setting up the matrix?

Yes, as an example

What are the other 2 methods doing?

Which methods ??


Note that is difficult to answer clearly because you don't give us a lot of explanations on what you really want to do...
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok well really all i want to do is pass the values i have in a two dimensional array into another class which contains another method, which will be used for calculations basedon these values. That's all. I just want to reference the matrix
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seb has provided an example. There are several other ways to go about it. Which way is "best" is dependent upon your design. Try out Seb's idea first, and see if that works for you.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are at least two options that I see. First you can pass a 2D array directly to your other class:

The other option is as illustrated above. If you have a class that represents a Matrix and provides all the typical mathematical operations, you can make a method that takes your custom Matrix class:

I think the later is more desirable if you are thinking of the 2D array as a Matrix and want to manipulate it as such (like calculating the determinant, adding two matrices, etc.).

Unfortunately, I cannot give any more detail since your question seems a little vague. I think this is why you may be confused with Seb's example. It will help a lot if you post some of your own code to illustrate what you have done so far. At the very least, this will allow us to use the same class, method, and variable names that you use.

Layne
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, in one java file I have this...

//Attributes
int column;
int row;
int match = 1;
int mismatch = -1;
int X = 4;
int Y = 4;
int[][]subMatrix = new int[X][Y];

for(row = 0; row < X; row++){
for(column = 0; column < Y; column++){
if (column==row){
subMatrix[row][column] = 1;}
else {subMatrix[row][column] = -1;}

}
}

new PrintElement().printSomething(subMatrix);

Now, I want to pass the values that are in this 2d array into a different java file which will have a method that will use the data in the arrays in an algorithm. The question is...how do i get the other file to reference each array element?
[ October 24, 2005: Message edited by: Sam Bluesman ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, the code you posted is not complete. What class and method is this code in? When I asked for code, I was more interested in class structure (field and methods) rather than the details of the method implementations. Also, it will help if you show what you have in the "other file". In particular, what is the class name? What methods and fields does it have?

The idea here is that you are looking at design issues. When we talk about design, we typically ignore the details inside of a method. We need to talk about the classes, methods, and data fields instead.

Also, when you post code, you should use UBB
Of course, this might not be the best way to do it. I think we need to discuss the design further before we go into implementation details like this.

I have one last general comment about your code. It is customary to declare variables when they are first used, especially with for loops. For example, I think you should change

to

This typically makes code much more readable. It also helps you to avoid reusing variables when you shouldn't.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic