Apologies about this being a two-part video, AND for my mic volume being weird, in future videos this is fixed. I’ll consider re-recording this later


Topics Discussed: Using math functions with if-based logic to choose the output of a program

Source code for Lesson 8, Part 1:

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;
 
        System.out.println("Enter a value for the variable: ");
        choice = input.nextDouble();
 
        //even
        if((choice%2) < 1){
            System.out.println("The number " + choice + "  is even prior to the decimal point. ");
        }
        else{
            System.out.println("The number " + choice + "  is NOT even. ");
        }
 
        //perfect square
        if((Math.sqrt(choice)) == 0){
            System.out.println("The number " + choice + "  is a perfect square: ");
        }
        else{
            System.out.println("The number " + choice + " is NOT a perfect square: ");
        }
 
        if ((choice%1) == 0){
            System.out.println("The number " + choice + " is a whole number: ");
        }
        else {
             System.out.println("The number " + choice + " is NOT a whole number: ");
        }
 
        if((((Math.pow(choice, 2)))%2) == 0){
            System.out.println("The number " + choice + " squared is even: ");
        }
        else{
             System.out.println("The number " + choice + " squared is NOT even: ");
        }
 
    } //End main
} //End class

8-2 Here


Topics discussed: Same as above

Source Code for Lesson 8, Part 2:

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;
 
        System.out.println("Enter a value for the variable: ");
        choice = input.nextDouble();
 
        //even
        if((choice%2) < 1){
            System.out.println("The number " + choice + " is even prior to the decimal point. ");
        }
        else{
            System.out.println("The number " + choice + " is NOT even. ");
        }
 
        //perfect square
        if(((Math.sqrt(choice))%1) == 0){//sqrt(9) == 3
            System.out.println("The number " + choice + "  is a perfect square: " + Math.sqrt(choice));
        }
        else{
            System.out.println("The number " + choice + " is NOT a perfect square: " + Math.sqrt(choice));
        }
 
        if ((choice%1) == 0){
            System.out.println("The number " + choice + " is a whole number: ");
        }
        else {
             System.out.println("The number " + choice + " is NOT a whole number: ");
        }
 
        if((((Math.pow(choice, 2)))%2) == 0){
            System.out.println("The number " + choice + " squared is even: "+ Math.pow(choice, 2));
        }
        else{
             System.out.println("The number " + choice + " squared is NOT even: " + Math.pow(choice, 2));
        }
 
    } //End main
} //End class

In these videos I talk a little bit about java.lang.Math; An include that allows us to do some simple mathematical calculations in our code in conjunction with the if-statements we’ve been talking about.

java.lang.Math functions
There are a LOT of different constructs in the java.lang.Math library, but the ones we use in this lesson seem to be the most common of them. Raising a number to a power, and taking the square root of a number. The code for each of these is as follows: (assuming you have the import in your code)
Examples:

Math.pow(numberOrVariable, powerRaisingTo);
Math.pow(x, 3); // raises x to the 3rd power
Math.sqrt(x); // Gets the square root of X

 

 

Last modified: April 9, 2019

Author