• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a method

 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks, I have a program which i've typed out of a book, but can't quite seem to wrap my head 'round.
OK, the class RangeClass contains the method <B>makeRange</B> and a main method.
If we look at the makeRange method:
Its return type is an array of integers. It accepts 2 parameters - two integers - upper and lower. It then creates an array called arr, of type integer, and consists of the value of the upper - lower, the result of which is increased by the value of 1.
Then, we have a for loop which increases by a value of 1 (i++) to the length of the array. But the length of this array is 10 [(10-1) +1], right??
Now, back to the main method.
We firstly define an array theArray of type int.
Then we define a new instance of RangeClass.
Then, we pass the values 1 & 10 to the makeRange method using the line:-
theArray = theRange.makeRange(1, 10);
Uhm, i've got completely lost at this point.

Basically, can somebody PLEASE tell me what is going on?
Sorry for such a long-winded post, but there really was no other way (I think).
Cheers in advance.
[ May 01, 2003: Message edited by: Steve Jensen ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Jensen:
Its return type is an array of integers. It accepts 2 parameters - two integers - upper and lower. It then creates an array called arr, of type integer, and consists of the value of the upper - lower, the result of which is increased by the value of 1.
Then, we have a for loop which increases by a value of 1 (i++) to the length of the array. But the length of this array is 10 [(10-1) +1], right??

Almost. You are correct when you state that the array us created with an initial length of 10. However, after you create the array, the [] syntax is used for array access, not array length. Therefore the line


does not increase the length of the array, but rather assigns the value at arr[ i ] to the value of lower (and then increments lower by one). The result of this will be...
...what you see in the main method. System.out.print tells the console window (or whatever your standard out is...) to display a message. The loop then loops through the array and uses the [] syntax to access the element at that position in the array to display that element on the screen.
Note that the last call to System.out is actually println. The difference between print and println is that println will append a newline after it finished printing.
So, running the program as is would produce:
The array: [1 2 3 4 5 6 7 8 9 10]
[ May 01, 2003: Message edited by: Joel McNary ]
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Steve
the length of this array is 10 because [(10-1) +1], right??

Yes,right.

Then, we pass the values 1 & 10 to the makeRange method using the line:
theArray = theRange.makeRange(1, 10);(a)
Uhm, i've got completely lost at this point.

Basically, can somebody PLEASE tell me what is going on?

In (a) we use the object of theRange to call the method of makeRange(1,10),after the processing the logic operation, the method of
makeRange(1,10) return the array of integer data type to

So, the result as following
The array: [ 1 2 3 4 5 6 7 8 9 10 ]
If it is not clear,please post it again
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhm, ok. :roll:
But in the main method, what's going on here:-



What's the for loop doing?? I mean, I know how this for loop works, but in relation to the program - I can't see it.
[ May 02, 2003: Message edited by: Steve Jensen ]
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Steve
Please look at the code

OK
(a)I were talking before,so I general describle it that the object of theRange call makeRange() method,after finish the operation,the method return integer array to the variable. //This point OK?
(b)Because the method of makeRange return the integer array to theArray varible,so theArray has the same size of arr array(c).
Then theArray.length=10,and the firsly element in theArray[0] variable value is 1
then print it out with the "" means blank space
and it will continue to do ten times and the second times the index of theArray is 1 and the value in the theArray[1] is 2
If this is not clear,please post it again
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errr, yeah, I guess it makes sense now.

I guess i'll just keep plodding along with it, it might click that way, then.
Cheers everybody for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic