• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help to resolve the error in the program.  RSS feed

 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem Statement:
We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble





Error Displayed:

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variable amonkey is not static so it can't be accessed from static method main.
If you want to access it, make it static or access them from your cms viz cms.amonkey.
Or even better. Don't pass any argument into checkSmile method and just read instance variables.

And this is from a problem from CodingBat ;)

BTW, instead of writing...... you can just write
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is a CodingBat question, then you are writing a function. You want it to return the appropriate result depending on the parameters; no fields no nothing. The solution given on CodingBat is very poor.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to call it like this
java MonkeyBusiness true false
…or similar.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I had to change a ) to " in my code to avoid a syntax error.
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pardon my butting in, but I think the operater you need is exclusive-or "^", and you'll need to negate that to get the correct result.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Schuetz wrote:Pardon my butting in, but I think the operater you need is exclusive-or "^", and you'll need to negate that to get the correct result.


And as Pawel said earlier, that can be written as
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But neither of those is the best answer.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote: . . . And as Pawel said earlier, that can be written as . . .
I can no longer find the Sun/Oracle style guide, but you can read about such code here. And the author Tomasz Nurkiewicz, quotes that as one part of the Sun guide he actually agrees with
So surely it is not so much, “can be written,” as, “should be written,” like that.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry: wrong link: that won't work; they seem to have removed it. Try here and use ctrl‑F “10.5.2”
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!