Tutorial On Boolean's

Discussion in 'Programming General' started by iJava, Nov 30, 2011.

Tutorial On Boolean's
  1. Unread #1 - Nov 30, 2011 at 5:52 PM
  2. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Tutorial On Boolean's

    Hi,

    Welcome to my first Java tutorial. In this tutorial I will teach you the basics of booleans.


    First of all,

    What is a boolean?

    A boolean is a variable used in the JAVA programming language which stores a true or false value.

    When do I use booleans?

    You can use a boolean when you want to check if something is in the right place or to check if something is right or wrong(true or false), for example :

    Code:
    public class test {
    public boolean isHeOld = false;
    public int age = 67;
    
    public void isHe() {
    if(age > 50) {
    isHeOld = true;
    } else {
    isHeOld = false;
    }
    }
    
    That's an example of how we could use a boolean.


    Now a boolean can store two values, "true", "false". We can use them in many different ways.

    To start off you need to declare and instiliaze the boolean :

    Code:
    public class test {[COLOR="Red"]
    public boolean isItThere = false;[/COLOR]
    }
    We can now use the isItThere boolean.

    We could use it in an if, while, almost any kind of statement.

    For example :

    Code:
    public class Test {
    public boolean isItThere = false;
    
    [COLOR="Red"]public void isHe() {
       int currMouseX = MouseInfo.getPointerInfo().getLocation().x;
            int currMouseY = MouseInfo.getPointerInfo().getLocation().y;
            SpinnerNumberModel model1 = (SpinnerNumberModel) x1.getModel();
            SpinnerNumberModel model2 = (SpinnerNumberModel) y1.getModel();
            int xInt1 = model1.getNumber().intValue();
            int yInt1 = model2.getNumber().intValue();   
    if(currMouseX == xInt1 && currMouseY == yInt1) {
    isItThere = true;
    } else {
    isItThere = false;
    }
    }[/COLOR]
    }
    This will check the mouse coordinates and give the variable a true or false value depending on where the mouse is.

    Another way we can use this is....

    If we want to check if something is true, i.e the mouse is in the correct position, then we could use some code as seen below :
    Code:
    public boolean atP1() {
            int currMouseX = MouseInfo.getPointerInfo().getLocation().x;
            int currMouseY = MouseInfo.getPointerInfo().getLocation().y;
            SpinnerNumberModel model1 = (SpinnerNumberModel) x1.getModel();
            SpinnerNumberModel model2 = (SpinnerNumberModel) y1.getModel();
            int xInt1 = model1.getNumber().intValue();
            int yInt1 = model2.getNumber().intValue();        
            return (currMouseX == xInt1 && currMouseY == yInt1);
        }
    
    This means that if the current mouse position = the xInt1 and yInt1 then it will return true. We can call on these methods like such,
    
    [CODE]
    if(atP1()) {
    //We're there and it returned true
    } 
    else if(!atP1()) {
    //We're not there and it returned false
    }
    Now to use the variable we could do something like :

    Code:
    public class Test {
    public boolean isItThere = false;
    
    public void isHe() {
       int currMouseX = MouseInfo.getPointerInfo().getLocation().x;
            int currMouseY = MouseInfo.getPointerInfo().getLocation().y;
            SpinnerNumberModel model1 = (SpinnerNumberModel) x1.getModel();
            SpinnerNumberModel model2 = (SpinnerNumberModel) y1.getModel();
            int xInt1 = model1.getNumber().intValue();
            int yInt1 = model2.getNumber().intValue();   
    if(currMouseX == xInt1 && currMouseY == yInt1) {
    isItThere = true;
    } else {
    isItThere = false;
    }
    }
    
    [COLOR="Red"]public void doStuff() {
    if(isItThere == true) {//We could also do "if(isItThere) {" - Do the same thing
    System.out.println("We're There");
    }
    if(isItThere == false) {//We could also do "if(!isItThere) {"-Do the same thing
    System.out.println("We're not there");
    }[/COLOR]
    }

    Well I hope you understood some of that as it is the basics to using booleans in Java.

    Enjoy!
     
  3. Unread #2 - Nov 30, 2011 at 8:35 PM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Tutorial On Boolean's

    Two things quick issues:
    • You should code to the java coding conventions
    • Booleans and other primitive types can't be null
     
  5. Unread #3 - Dec 1, 2011 at 1:21 AM
  6. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Tutorial On Boolean's

    I whipped this up fast so it may not follow the conventions. I fixed the second as I got confused with a String, don't ask me why.
     
  7. Unread #4 - Dec 1, 2011 at 6:53 AM
  8. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    Tutorial On Boolean's

    How on earth is someone going to understand the code you gave if they don't understand what a boolean is?
     
  9. Unread #5 - Dec 1, 2011 at 3:24 PM
  10. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Tutorial On Boolean's

    I gave a brief explanation and they can always use Google.
     
  11. Unread #6 - Dec 1, 2011 at 3:31 PM
  12. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    Tutorial On Boolean's

    So you want them to check a tutorial for your tutorial?

    If you want to explain the basics, stick to the basics.
     
  13. Unread #7 - Dec 1, 2011 at 3:54 PM
  14. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Tutorial On Boolean's

    No, look http://lmgtfy.com/?q=What+Is+A+Boolean+Java now click the first link and it has a greater explanation on a boolean. If someone is going to learn Java then they should be willing to work hard.
     
  15. Unread #8 - Dec 1, 2011 at 4:40 PM
  16. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    Tutorial On Boolean's

    So you're basically saying there is no purpose to your tutorial as it would be better to google "what is a boolean"?

    Edit: I'm not going to post anymore, as I hate it when people argue on my threads - so I won't on yours - it was just a suggestion to keep it simple rather than over-complicating a simple issue.
     
< Paying for someone to make a rather simple program | GetBetween (get between two strings!) >

Users viewing this thread
1 guest


 
 
Adblock breaks this site