Topics Discussed: Boolean Logic / Operators

Source Code for Lesson 9:

import java.lang.Math;
import java.util.Scanner;
class Jtutorial1 {
    public static void main(String args[]){
     Scanner input = new Scanner(System.in);
        // Declare a double, run some tests on it.
        // Is it even or odd?  Is it a perfect square?   Is it a whole number?
        // Is the number squared even?  
 
        //<, <=, >, >=, || ,  &&,  !=
 
 
        double choice=0;
        boolean isEven=false, isPS=false, isWhole=false, isSquaredEven=false;
 
 
        System.out.println("Enter a value for the variable: ");
        choice = input.nextDouble();
 
        //even
        if((choice%2) < 1){
            isEven=true;
            //System.out.println("The number " + choice + " is even prior to the decimal point. ");
        }
        else{
            isEven=false;
            //System.out.println("The number " + choice + " is NOT even. ");
        }
 
        //perfect square
        if(((Math.sqrt(choice))%1) == 0){//sqrt(9) == 3
            isPS=true; 
            //System.out.println("The number " + choice + "  is a perfect square: " + Math.sqrt(choice));
        }
        else{
            isPS=false;
            //System.out.println("The number " + choice + " is NOT a perfect square: " + Math.sqrt(choice));
        }
 
        if ((choice%1) == 0){
            isWhole=true;
            //System.out.println("The number " + choice + " is a whole number: ");
        }
        else {
            isWhole=false;
             //System.out.println("The number " + choice + " is NOT a whole number: ");
        }
 
        if((((Math.pow(choice, 2)))%2) == 0){
            isSquaredEven=true;
            //System.out.println("The number " + choice + " squared is even: "+ Math.pow(choice, 2));
        }
        else{
            isSquaredEven=false;
             //System.out.println("The number " + choice + " squared is NOT even: " + Math.pow(choice, 2));
        }
 
        if(isEven){
            System.out.println("The number is even. ");
        }
        if(isPS){
            System.out.println("The number is a perfect square. ");
        }
        if(isWhole){
            System.out.println("The number is whole. ");
        }
        if(isSquaredEven){
            System.out.println("The number sqared is even. ");
        }        
 
 
 
 
    } //End main
} //End class

 

 

HomeWork for Lesson 9:

/*
      * See math operations in java here:  https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
      * Each of these operators can be called like I called Math.pow (ex: Math.abs(variable) ) 
      * 
      * Problems that you should try to solve-- Pick one or more!
      * A:  Allow the user to enter a telephone number, use an if statement to invalidate numbers that are too long / short in length
      * if the telephone number is within the right length, seperate the numbers appropriately into how they would appear in your
      * country of origin:  Ex. 7818789000 would turn into 781-878-9000 in the USA.  (This won't require much but mod / division)
      * Hint:  Use long or double if you need to use a big number.
      *
      * B: Write a program that will allow you to input a number to select an operation (output a menu, then allow the user to select one).
      * In that menu you should be allowing the user to choose a shape (circle, square, rectangle, etc).  Once they select it, ask them to
      * input the dimensions of it (circle can be radius, diameter, circumferece, rectangle can be length, width, perimeter or area.... etc etc.)
      * Once you have taken in a few points of input, calculate the missing pieces of information.   Perhaps someone has a rectangle
      * where they have the area and the length, but not the width.  Using java, figure out what the missing width is.
      * 
      * C:  Extra credit for people that are antsy to get some after the last 2 assignments:
      *     write a program that determines the last digit of 3^n power (3^3 = 27, thus 7).  Hint, you can use n%4 to help you on this one. 
      */

 


Boolean operators
Boolean operators are variables which have two states, “true” or “false” (default is false if you use a simple declaration). These can be used in a variety of ways, but are most typically seen when you need to confirm that something happened in an if statement or a loop (in later lessons). Here’s a few examples:

int empPay1=0;
boolean isChanged;//ischanged defaults to false
System.out.println("Enter the income for employee 1 that is greater than 0.");
empPay1= input.nextInt(); // Enter pay
if (empPay1 > 0){// if the value is > 0, meaning it's been changed
    isChanged=true;
}
if(isChanged){//if the value has been changed (by the if above)
    System.out.println("empPay has been changed");
}//You could just use an else here, but instead you could use the following:
if(!isChanged){
    System.out.println("empPay has not been changed");
}

In the above example I show that booleans can be used in place of the standard if conditionals and used as a simple true / false that is set as part of another conditional. At present these must seem convoluted, but in the future when our variables change around a bit more it will make more sense to use these than other pieces of code.

Last modified: April 9, 2019

Author