Block B

Here we see some Double methods at work.


jshell> Double d = 1.5;
d ==> 1.5

jshell> d.toString()
$2 ==> "1.5"

jshell> Double.toString(d)
$3 ==> "1.5"

jshell> Double.toString(3.14159)
$4 ==> "3.14159"

The most interesting Character methods are the methods that test if a character is of a certain type.


jshell> Character.isAlphabetic('a')
$5 ==> true

jshell> Character.isAlphabetic('5')
$6 ==> false

jshell> Character.isDigit('5')
$7 ==> true

jshell> Character.isDigit('a')
$8 ==> false

jshell> Character.isISOControl('\n')
$9 ==> true

jshell> Character.isISOControl('\t')
$10 ==> true

jshell> Character.isISOControl('p')
$11 ==> false

Here is the best way to check if a character is whitespace.


jshell> Character.isWhitespace(' ')
$12 ==> true

jshell> Character.isWhitespace('\n')
$13 ==> true

jshell> Character.isWhitespace('\t')
$14 ==> true

jshell> Character.isWhitespace('q')
$15 ==> false

Now lower yourself.



jshell> Character.toLowerCase('a')
$16 ==> 'a'

jshell> Character.toLowerCase('A')
$17 ==> 'a'

jshell> Character.toLowerCase('6')
$18 ==> '6'

Here we promote a character to a one-character string.



jshell> Character.toString('a')
$19 ==> "a"

jshell> Character.toString('\n')
$20 ==> "\n"

Dare to compare by subtracting ASCII values.



jshell> Character.compare('a', 'z')
$22 ==> -25

jshell> 

Conditional Logic

This works in a manner entirely similar to that of Python, modulo cosmetic differences. The rules for the if, else and else if boss statements are thame as those for Python's if, else, and elif. Note that predicates must be housed in parentheses.


public class Iffy
{
    public static void main(String[] args)
    {
        int entered = Integer.parseInt(args[0]);
        if(entered < 0)
        {
            System.out.println("You entered a negative number");
        }
        else if(entered == 0)
        {
            System.out.println("Zip City!");
        }
        else
        {
            System.out.println("You entered a positive number");
        }

    }
}

Here is how to imitate Python's input.


import java.util.Scanner;
public class Hoover
{
    public static void main(String[] args)
    {
        Scanner vacuum = new Scanner(System.in);
        System.out.print("Enter a integer:  ");
        String foo = vacuum.next();
        double x = Double.parseDouble(foo);
        System.out.printf("The square of %s is %s.\n", x, x*x);
    }
}

Java has a switch-case statement; it is demonstrated here. Each case needs a break statment so only one case exeecutes. Omit these and see what happens.


public class Case
{
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        switch(n)  //you can switch on integral types
                   //any of the four integer types
                   //strings
                   //chars
                   //enums (later)
        {
            case 0:
                System.out.println("You entered a zero.");
                break;
            case 1:
                System.out.println("You entered a one.");
                break;
            case 2:
                System.out.println("You entered a two.");
                break;
            default:
                System.out.println("Having a default is optional");
        }
    }
}

Here is the new (Java12+) case stement. I think it's tons nicer. And we get rid of those irksome break statements.


public class NewCase
{
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        switch(n)  //you can switch on integral types
                   //any of the four integer types
                   //strings
                   //chars
                   //enums (later)
        {
            case 0 -> System.out.println("You entered a zero.");
            case 1 -> System.out.println("You entered a one.");
            case 2 -> System.out.println("You entered a two.");
            default -> System.out.println("Having a default is optional");
        }
    }
}