• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting an index's boundaries  RSS feed

 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have to create a method that takes int index. I am getting testing errors, because the index must be set 'between zero, inclusive, and the number of vertices, exclusive.'

So far all I have is:



Im pretty lost on how to "set" the index. If you can help it would be appreciated. Thanks.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to control the same at calling time... You need to make sure that passed index should be between 0 to index - 1 if index is 1 to index else
if index is starting from 0 then you can pass index directly
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So should I make an if statement that basically says if index is >= number of vertices then return vertices[index].?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no..

You can say like this:



or


 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jackson Daniels wrote:So should I make an if statement that basically says if index is >= number of vertices then return vertices[index].?


You could do that, but then you'd have to decide what to return if that test failed. I don't think that's what you're missing here, though. The way you "set" that parameter is by doing it before you call the method. An example which is simpler than Tushar's, a bit simpler anyway:



So here I have set the value of the parameter to 1. Of course I could have any kind of code at all which sets the value of the parameter, I could read it from the console or choose a random number or use a loop variable as per Tushar's examples. Does that help, or are we on the wrong track?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to create a method that takes int index.

This part has nothing to do with setting the index. So your comment
So should I make an if statement that basically says if index is >= number of vertices then return vertices[index].?
is in the right direction, but incorrect logic. What you need is something like this:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!