How to call an array method I wrote from another class:?
-
This is the array method I wrote to assign random numbers to an array: public SpreadSheet(int r, int c) { r = rows; c = cols; int [][] sheet= new int [r][c]; for (r=0; r < rows; r++) { for (c=0; c < cols; c++) { sheet[r][c] = (int)(100*Math.random()); }//end of loop for c }//end of loop for r } This is how I am trying to call it in another class with (5,7) 5 rows 7 columns: SpreadSheet whichSheet = new SpreadSheet(5,7);
-
Answer:
You shouldn't set r and c equals to rows and cols. You pass 5 and 7 into the method; now r = 5, c = 7. And then in your method, you set r and c equals to another variable, r and c no longer contains 5 and 7, respectively. Second, the variables rows and cols never declared. Declare the 2 variables and than set rows and cols equals to r and c, respectively. public SpreadSheet(int r, int c) { int rows = r; int cols = c; int[][] sheet = new int[rows][cols]; for (int ROWS = 0; ROWS < rows; ROWS++) { for (int COLS = 0; COLS < cols; COLS++) { sheet[ROWS][COLS] = (int)(100*Math.random()); } } } And then you call the method in another class passing 5 and 7 into the method's parameter.
cory at Yahoo! Answers Visit the source
Other answers
Where are the variables rows and cols defined? You are passing in rows and columns as parameters. Use them. I would rename the parameters from r and co to rows and cols. public SpreadSheet(int rows, int cols) Then, you need loop variables, use r and c for those: int r, c; Declare the array using the parameters, not the local variables: int [][] sheet= new int [rows][cols];
Ratchetr
the method named "SpreadSheet" is obviously a constructor, and it appears your call to it is in order, assuming it compiles.
halrosser
Related Q & A:
- how to How to change button properties from another class?Best solution by stackoverflow.com
- How to call a popup from a link inside another popup?Best solution by stackoverflow.com
- How to use an object created in Main class with another created in another class?Best solution by stackoverflow.com
- How to call delegate methods from other class in Swift?Best solution by stackoverflow.com
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.