Results 1 to 7 of 7
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Boolean methods & how to read this in plain English

    Hello again,

    I have another question.

    From the following method declaration and implementation:

    Java Code:
     public static boolean isValidRank(int rank) {
            return ACE <= rank && rank <= KING;
        }
    First question: Why doesn't the method return true or false? Isn't that what the type call for? Shouldn't this generate a compile time error?

    Second question: How do you read this?

    My initial thought was: Declaring a static boolean type method name isValidRank which accepts as a parameter the integer rank and it returns ACE less or equal to rank AND rank is less or equal to KING but the second part (red color font) made non sense. So what gives?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bigsonny View Post
    First question: Why doesn't the method return true or false? Isn't that what the type call for? Shouldn't this generate a compile time error?
    It returns the result of a boolean expression which is true or false.


    Second question: How do you read this?

    My initial thought was: Declaring a static boolean type method name isValidRank which accepts as a parameter the integer rank and it returns ACE less or equal to rank AND rank is less or equal to KING but the second part (red color font) made non sense. So what gives?
    ... that takes an int parameter, rank, and returns true if rank is greater than or equal to ACE and less than or equal to KING. Otherwise it returns false.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    [QUOTE=Fubarable;216364]It returns the result of a boolean expression which is true or false.[
    /quote]

    So it doesn't have to return the words true or false? Also, which part is the boolean expression? Does it start from the method declaration or is it only the code between the brackets? How does this get evaluated without a conditional? There is no if.

    Shouldn't the code be:
    Java Code:
    public static boolean isValidRank(int rank) {
    if ((rank >= ACE) && (rank <= KING)){
    return true;
    }
    else return false;
    }
    ... that takes an int parameter, rank, and returns true if rank is greater than or equal to ACE and less than or equal to KING. Otherwise it returns false.
    Thank you. This is clearer, though I would have read it from right to left, but ultimately, it's the same thing. Thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bigsonny View Post
    So it doesn't have to return the words true or false?
    Nope


    Also, which part is the boolean expression? Does it start from the method declaration or is it only the code between the brackets? How does this get evaluated without a conditional? There is no if.
    If is not a boolean expression but rather if can evaluate a boolean expression (the thing in the parenthesis immediately after the if keyword.

    So these are all boolean expressions:
    Java Code:
    1 == 2  
    true == false
    (someNumber < = x) 
    (someNumber > y)
    (someNumber <= x) && (someNumber < y)
    true

  5. #5
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Fubarable View Post
    Nope




    If is not a boolean expression but rather if can evaluate a boolean expression (the thing in the parenthesis immediately after the if keyword.

    So these are all boolean expressions:
    Java Code:
    1 == 2  
    true == false
    (someNumber < = x) 
    (someNumber > y)
    (someNumber <= x) && (someNumber < y)
    true
    Thank you. Would the code I wrote be redundant? Would it even work?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bigsonny View Post
    Would the code I wrote be redundant?
    Yes.


    Would it even work?
    Best to run and test it yourself. :)

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Fubarable View Post
    Best to run and test it yourself. :)
    True indeed. Thank you very much for you help!

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. SwingWorker or just plain thread
    By elin in forum New To Java
    Replies: 2
    Last Post: 07-09-2010, 01:26 AM
  3. Replies: 3
    Last Post: 06-08-2010, 08:10 PM
  4. Boolean methods
    By sonny in forum New To Java
    Replies: 7
    Last Post: 03-26-2010, 10:16 PM
  5. Font.PLAIN - won't set font style to PLAIN!
    By Unsub in forum New To Java
    Replies: 3
    Last Post: 03-11-2010, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •