• Post Reply Bookmark Topic Watch Topic
  • New Topic

My boolean solution always returning false on learneroo.com website.  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My challenge on the site was: "You will be given two integers a and b. Return true if a is a multiple of b, and false otherwise."

My code is below but when I hit the 'run code' button on the website all the examples are return false values which is incorrect:


[ Moderator edit: added code tags ]
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's what I get when I run it:
C:\slop>java Main
2
5
10
true
3
21
true

What EXACTLY are you entering?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Drummond wrote:My code is below...

Just a word of advice: Don't write methods like 'doStuff()'; it's a really bad habit to get into. Give it a proper name.

You've already described it in the variable you define for it (but never use).

Tip: One convention that's quite widely used is that names for methods that return a boolean start with the prefix "is".

Winston
 
Alex Drummond
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice guys

I actually sorted it now, I had the 'a' and 'b' variables the wrong way AROUND! I feel silly.

Also I should have mentioned the website gave me most of that code I only had to fill in the small section inside 'dostuff' and I already had inputs and expected outputs given to me:

Input Correct Output Your Output
6, 3 true True
10, 3 false false
17, 5 false false

Thanks for helping guys, everyone on here is truly awesome
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Alex!

This:

can be more succinctly written as this, the doStuff name notwithstanding:

The local variable multiple is actually not needed so it can be deleted from that method.

Of course, a better name for this method would be isDivisible() instead of doStuff(). If you assign meaningful names, the code becomes:

When you have better names, you'll see that the parameter order declared is not very natural. Numerator more naturally precedes denominator, so finally, you would have:

You could also try the name hasNoRemainder instead.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the other hand, I realized that you had an unfulfilled intent in declaring the multiple variable, so the method might be better with these names:

Which says, "Tell me if the candidate number is a multiple of the given number". Notice that the parameter ordering semantics now goes back to what you had originally.

Do you see how important good names are to making your program clearly communicate what it's trying to do and how it's supposed to work?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!