I am recent in Java, I can read and understand code, know the high level (can do UML) and just recently I finally started to understand how to structure code so I can start writing.
I need help for the following, which will provide me another milestone into the understanding of the code
NOTE: if anyone can point to a tutorial that would explain with full examples (not only a constructor out of context, or a class, but also for that example how to call it, and what is returned..., I would gladly appreciate.
for now that would help me.
also, if I am steering in the wrong direction, please let me know and correct me in a line or two.
I have a main, where the input is taken from the user for 1 value (this valuewill define the rows and colons for an array) = this is ok.
I need to create the array.
then I need to populate it with random numbers
(after that I will add more manipulations on it, but then I will be able to do so, as long as I pass that stage)
from that value I will need to generate more arrays (with derived sizes) but leave them with null values.
now I already did that in my main, but that is linear programming, and once again: I need to bridge the gap between UML and finished code.
for that I went the way of
1_ creating a class MyArr
2_ create a constructor with takes an int mySide as argument and use the int to create a 2 dimension array[mySide][mySide]
as I understand, when called from the main, that should instantiate an object of MyArr class.
3_ create a method "populate" which can be called from main with the object instantiated passed to it as an argument.
it takes that object and populate the array created (not certain of the terminology here)
in other words, seen from the main.
create a new instance of the Myarr (which in fact is / contains an array of the size passed in call.
then if needed, call Myarr.populate(????) that will populate that array.
then I can create another instance of Myarr with a different size provided and not populate it (or leave it filled with null
I paste a skeleton.
as you can see, I left empthy most of the components for the class
but creating the array when the class is instantiated would look like:
int[][] a1 = new int[mySide][mySide]
then calling it would be something like:
MyArr baneArr = new MyArr(mySideInt);
and here is the first problem I face: baneArr will be an object.
first question: is that correct?
if yes, then second step:
populate will be a nested loop with random numbers (I have no problem there besides how to access that object's array?
something like:
myArr.populate(baneArr)
and if that is the way how to I use it in populate:
baneArr[counterX][counterY] = myRandomNumber ?
I tried many ways over the past week end, and still face too many issues where I am not passing the correct type of argument, or if I fix that then I cannot set the values in the command described above.
I have 15 years in telecoms, and never touched programming besides reading code, and heavy bash /perl scripting. this thing is not part of a project, nor an assignment, it is the only way I can understand this and a single example would be very good to help me move forward. I learned everything by taking examples and playing with them, unfortunately all the example/tutorial I see beyond hello world and such only show pieces of code without the full working code. then it jumps to code so complex that is it beyond reverse engineering for intermediate learners. in between I cannot find information that would fill the gap, and I have been looking for years; this time I am not giving up. please help
public class Main {
public static void main(String[] args) {
// take the size the user wants
String mySizeStr = JOptionPane.showInputDialog(null, "enter the required size", "input the size",JOptionPane.QUESTION_MESSAGE);
// get the square root for that given size
double mySringDbl = Double.parseDouble(mySizeStr);
double mySideDbl = Math.sqrt(mySringDbl);
// convert it to an int (required for an array)
int mySideInt = (int) mySideDbl;
// call MyArray ???
}
public class MyArr {
// here I need a constructor
public MyArr(int size){
}
public void populate( ???? ){
}
}
Edited by macosxnerd101: Welcome to DIC!
This post has been edited by macosxnerd101: 24 June 2010 - 09:13 AM

New Topic/Question
Reply




MultiQuote







|