• Post Reply Bookmark Topic Watch Topic
  • New Topic

If statements  RSS feed

 
john clarke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys, Im new to java and this is my first post.
Just have a question with if statements

so I know that the condition of the if statement has to be a boolean value, but here is the problem Im having.

Im making a turn based soccer game, which asks the player to pass or shoot the ball.

right now, Ive defined a method called hasBall(), which sets the variable "player" to 1, if the team is in possession of the ball. I made an object called team1, so when I want the object to use that method, I just go team1.hasBall()


I cant continue my program unless I make an if statement, with the condition being whether team1 has the ball or not.

I want to do this: if(team1.hasBall)
System.out.println("blah blah blah")

but this doesnt work because the condition isnt a boolean value, so Im stumped. how can I achieve this? note that the variable "player" is set to private.

sorry If im being too vague, ill give you more info if needed. help will be much appreciated
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of setting the player variable equal to 1 if the pass has possession of the ball, you could return a value (true or false) depending on whether the player has the ball. That method would work in an if statement.

-Hunter M
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going off post number two.

 
john clarke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh thanks guys! I get it now
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Good idea of Brad Dwan's but the naming of the methods and variables is less than ideal. I would rename the variables something like this, but the name "setHasBall" could be improved still.Note the necessity for the keyword this when the parameter name and the field name are the same.

I can see a potential problem; you can use that method to set both teams, or neither team, as being in possession. An alternative would be to have a teamInPossession field in the Ball class, then you can alter the Team in the Ball class.Note the Exception if anybody tries to pass null; if I remember correctly the ball is in possession of one team or the other at every time during a match, even if possession can change very rapidly.
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Good idea of Brad Dwan's but the naming of the methods and variables is less than ideal. I would rename the variables something like this, but the name "setHasBall" could be improved still....

Subtle changes. Although good ones, cleans it up nicely and increases readability *note taken*. Very nice. Ta
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, if I'm already not following JavaBean conventions, I'd go the fluent interface route, and just define:and leave off the "set", since (in my opinion) it's implied by passing in a value. Some people don't like the fluent interface style (and normally the fluidic methods would return the current instance to allow chaining).
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Brad and David, for additional comments about the naming; I think we are in agreement there.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!