*
The moose likes Beginning Java and the fly likes Passing Parameters between classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Parameters between classes" Watch "Passing Parameters between classes" New topic
Author

Passing Parameters between classes

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
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


Moosey knows best
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Have you a class which represent your "matrix like structure" ?


Regards,<br />Seb<br /> <br />SCJP 1.4
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
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

Joined: Sep 28, 2005
Posts: 225
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

Joined: Nov 21, 2004
Posts: 191
Do you have any web links you could give me that would show me how to do that?
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
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

Joined: Sep 28, 2005
Posts: 225
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

Joined: Nov 21, 2004
Posts: 191
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

Joined: May 22, 2003
Posts: 657

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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
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

Joined: Dec 06, 2001
Posts: 3061
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Parameters between classes
 
Similar Threads
Difference between SAX and DOM??
Updating data in the JTable
Collections questions
variables one class accessed through another class???????
problem Recording Screencast of Selenium Tests using java ScreenRecorder jar API's