• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with boolean logic code  RSS feed

 
Java KidSt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a beginner with Java, and I'm trying to figure out an exercise. I'm supposed to use basic boolean logic. The concept is:

Enter 1st monster's name : Tom Enter 1st monster's size : 2 Enter 2nd monster's name : Bob Enter 2nd monster's size : 4

Monster 1 – Tom 2 Monster 2 – Bob 4

Monster one is smaller than Monster two. Monster one does not have the same name as Monster two.

Enter 1st monster's name : Sally Enter 1st monster's size : 7 Enter 2nd monster's name : Fred Enter 2nd monster's size : 2

Monster 1 – Sally 7 Monster 2 – Fred 2

Monster one is bigger than Monster two. Monster one does not have the same name as Monster two.

I have the first part ("Monster 1 Sally 7, Monster 2 Fred 2"), but I am not sure how to make the boolean comparisons work. i'm stuck. :-(

Using jcreator, I have a "Monster.java" tab that reads:



and a "MonsterRunner.java" tab that reads:

 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure your code compiles? Double check your toString() method on the Monster class. It should return a String representation of the current instance. If you implement the toString() method properly, you only need to write:


The names you are using right now, "test" and "go", don't fit what you're using them for. They don't help tell the story of what your code is doing. The names "monster1" and "monster2" will at least make your intent a little clearer.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this is poor form:

Write it this way instead:

Do you see why this is equivalent but more succinct and straight to the point?  (Edit: see below for why my versions are not actually equivalent to yours)

You could also write it like this:

 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should also compare the string using the equals method

and not use "!="
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:Do you see why this is equivalent but more succinct and straight to the point?

Actually, the versions I showed are NOT equivalent to your original code. You're returning false when the condition is met and true otherwise. Why are you doing that? That doesn't make sense.

If you have monster1 with size of 7 and monster2 with a size of 3, then I would expect the boolean expression monster1.isBigger(monster2) -- which I would read as "Is monster1 bigger than monster2?" -- to return true, not false!  Your code returns false though.

The same kind of problem exists in your isSmaller() code.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the reverse logic in your isBigger() and isSmaller() methods, since you're probably not familiar with conventions in Java and OO classes, you might be thinking that those read like "is the other monster bigger/smaller than this monster?"

While I can understand how you might think that, that's not how boolean methods like these are usually designed though. The current object (this) is usually the basis for comparison and the expression is interpreted from left to right.

That is, given an expression like o1.isGreater(o2), the normal convention is to read from left to right and insert the implied "than" to get "o1 is greater than o2?" or "Is o1 greater than o2?" not the other way around.

Look at the String class and methods like contains() and startsWith() as examples. If you have myName.startsWith("Jun") or myName.contains("lu"), these are interpreted as "Does myName start with 'Jun'?" and "Does myName contain 'lu'?" and not the other way around.

Another way to understand the convention is to read the code like this:

o1.isBigger(o2) - Is it true that o1 isBigger than o2?
o1.isSmaller(o2) - Is it true that o1 isSmaller than o2?
myName.startsWith("Jun") - Is it true that myName startsWith "Jun"?
myName.contains("lu") - Is it true that myName contains "lu"?
someList.isEmpty() - Is it true that someList isEmpty?

I hope that helps you better understand the convention.
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:That is, given an expression like o1.isGreater(o2), the normal convention is to read from left to right and insert the implied "than" to get "o1 is greater than o2?" or "Is o1 greater than o2?" not the other way around.

I agree Junilu, and funny enough when I read o1.isGreater(o2) I actually put the word "than" in the sentence without even thinking just out of habit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!