• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Help to resolve the error in the program.

 
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:

 
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • 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
 
Marshal
Posts: 65365
248
  • 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: 65365
248
  • 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: 65365
248
  • 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.
 
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.

 
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: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But neither of those is the best answer.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • 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: 65365
248
  • 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”
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!