Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 6th Edition
by Barry Burd

Chapter 10: Which Way Did He Go?

In this chapter:

Mysterious ways

When the computer executes
    if (firstSmaller = true) 
Java sees a single equal sign which is an assignment operator. So Java assigns the value true to the variable firstSmaller. So the if condition asks if firstSmaller is true. And, indeed, firstSmaller is true no matter what happened when the computer executed the earlier statement
    boolean firstSmaller = firstNumber < secondNumber;

So, with the condition in parentheses being true, Java executes the statement
    System.out.println("The first is smaller.");
This happens no matter what numbers the user types on the keyboard.

What kind of number?

import java.util.Scanner;

public class WhatKindOfNumber {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        int number;
        
        System.out.print("Enter an integer value: ");
        number = keyboard.nextInt();
        
        if (number > 0) {
            System.out.println("positive");
        } else if (number < 0) {
            System.out.println("negative");
        } else {
            System.out.println("zero");
        }
        
        keyboard.close();
    }

}

Approaching a traffic signal

import java.util.Scanner;

public class TrafficSignal {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        char approachingGreen;
        char safeToProceed;
        char officerDirectingNotToProceed;
        
        System.out.print("Are you approaching a green light? (Y/N) ");
        approachingGreen = keyboard.findWithinHorizon(".", 0).charAt(0);

        System.out.print("Is it safe to proceed? (Y/N) ");
        safeToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);

        System.out.print("Is is a traffic officer directing you not to proceed? (Y/N) ");
        officerDirectingNotToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        if (approachingGreen == 'Y' && 
                safeToProceed == 'Y' && 
                officerDirectingNotToProceed != 'Y') {
            System.out.println("Go");
        } else {
            System.out.println("Stop");
        }
        
        keyboard.close();
    }

}

"Yes" and "yes" again

import java.util.Scanner;

public class TrafficSignal2 {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        char approachingGreen;
        char safeToProceed;
        char officerDirectingNotToProceed;
        
        System.out.print("Are you approaching a green light? (Y/N) ");
        approachingGreen = keyboard.findWithinHorizon(".", 0).charAt(0);

        System.out.print("Is it safe to proceed? (Y/N) ");
        safeToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);

        System.out.print("Is is a traffic officer directing you not to proceed? (Y/N) ");
        officerDirectingNotToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        if ((approachingGreen == 'Y' || approachingGreen == 'y') && 
                (safeToProceed == 'Y' || safeToProceed == 'y') && 
                (officerDirectingNotToProceed != 'Y' && officerDirectingNotToProceed != 'y')) {
            System.out.println("Go");
        } else {
            System.out.println("Stop");
        }
        
        keyboard.close();
    }

}

Red or yellow light

import java.util.Scanner;

public class TrafficSignal3 {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        char color;
        char safeToProceed;
        char officerDirectingNotToProceed;
        
        System.out.print("What color is the traffic light? (G/Y/R) ");
        color = keyboard.findWithinHorizon(".", 0).charAt(0);

        System.out.print("Is it safe to proceed? (Y/N) ");
        safeToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);

        System.out.print("Is is a traffic officer directing you not to proceed? (Y/N) ");
        officerDirectingNotToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        if (color == 'G' && 
                safeToProceed == 'Y' && 
                officerDirectingNotToProceed != 'Y') {
            System.out.println("Go");
        } else {
            System.out.println("Stop");
        }
        
        keyboard.close();
    }

}

What? Another traffic signal program?

import java.util.Scanner;

public class TrafficSignal4 {

    enum Color {green, yellow, red}
    
    public static void main(String[] args) {    
        var keyboard = new Scanner(System.in);
        char colorOfLight;
        Color signal;
        
        System.out.print("What color is the traffic light? (G/Y/R) ");
        colorOfLight = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        if (colorOfLight == 'G') {
            signal = Color.green; 
        } else if (colorOfLight == 'Y') {
            signal = Color.yellow;
        } else {
            signal = Color.red;
        }

        System.out.print("The signal is ");
        System.out.print(signal);
        System.out.println(".");
        
        keyboard.close();
    }

}

Buying 3D glasses

import java.util.Scanner;

public class ThreeDGlasses {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        int age;
        double price = 0.00;
        char reply;
        int dimensions;

        System.out.print("How old are you? ");
        age = keyboard.nextInt();

        System.out.print("Have a coupon? (Y/N) ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);

        if (age >= 12 && age < 65) {
            price = 9.25;
            if (reply == 'Y' || reply == 'y') {
                price -= 2.00;
            }
        } else {
            price = 5.25;
        }

        System.out.print("How many dimensions: 2 or 3? ");
        dimensions = keyboard.nextInt();
        
        if (dimensions == 3) {
            price += 3.00;
        }
        
        System.out.print("Please pay $");
        System.out.print(price);
        System.out.print(". ");
        System.out.println("Enjoy the show!");

        keyboard.close();
    }
}

Going all /n

//out.println();

if (hankees > socks) {
    who = WhoWins.home;
    out.println("\nThe Hankees win :-)\n");
} else if (socks > hankees) {
    who = WhoWins.visitor;
    out.println("\nThe Socks win :-(\n");
} else {
    who = WhoWins.neither;
    out.println("\nIt's a tie :-|\n");
}

//out.println();