Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

BooleanDemo BlueJ Project

Click here to download a BlueJ project for BooleanDemo.


BooleanDemo Source Code

The Java source code for BooleanDemo is below. Click on a file name to download it.


BooleanDemo.java

/*
 * Example BooleanDemo: demonstrate a variety of constructs related
 * to boolean (true/false) data
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: BooleanDemo.java 2217 2013-10-18 14:00:31Z terescoj $
 */

import java.util.Scanner;

public class BooleanDemo {

    public static void main(String[] args) {

        // construct a Scanner for keyboard input
        Scanner input = new Scanner(System.in);

        // read in 3 numbers that will be used in subsequent comparisons
        System.out.print("Enter 3 integer values: ");
        int x = input.nextInt();
        int y = input.nextInt();
        int z = input.nextInt();

        System.out.println("Using x=" + x + ", y=" + y + ", z=" + z);

        // demonstrate the basic arithmetic comparisons, note that we can
        // print out the result of comparison operator
        System.out.println("x == y is " + (x==y));  // is x equal to y?
        System.out.println("x != y is " + (x!=y));  // not equal
        System.out.println("x < y is " + (x<y));
        System.out.println("x > y is " + (x>y));
        System.out.println("x <= y is " + (x<=y));
        System.out.println("x >= y is " + (x>=y));

        // boolean logical operators
        System.out.println("!(x < y) is " + !
        (x<y)); // not x<y
        // logical and -- both must be true for result to be true
        System.out.println("(x < y) && (y < z) is " + ((x < y) && (y < z)));
        // logical or -- both must be false for result to be false
        System.out.println("(x < y) || (y < z) is " + ((x < y) || (y < z)));
        
        // we can also declare boolean variables to hold true/false values
        boolean haveNegative = (x < 0) || (y < 0) || (z < 0);
        System.out.println("haveNegative is " + haveNegative);
        
        boolean allNegative = (x < 0) && (y < 0) && (z < 0);
        System.out.println("allNegative is " + allNegative);
        
        boolean allSame = (x == y) && (y == z);
        System.out.println("allSame is " + allSame);
        
        // we can use these boolean variables anywhere we would have a
        // boolean expression
        System.out.println("allNegative && allSame is " + (allNegative && allSame));
        
        // and we can combine these operators: && happens before ||
        System.out.println("(x < 0) && (y < 0) || (z < 0) is " + ((x < 0) && (y < 0) || (z < 0)));
        System.out.println("(x < 0) || (y < 0) && (z < 0) is " + ((x < 0) || (y < 0) && (z < 0)));
        System.out.println("((x < 0) || (y < 0)) && (z < 0) is " + (((x < 0) || (y < 0)) && (z < 0)));
        
    }
}