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

Chapter 9: Forks in the Road

In this chapter:

Oops!

The single equal sign stands for assignment, not for comparison. So instead of if (number = 12), the code should be if (number == 12).

Don't write code this way

The output of the code is as follows:
Will Java display this line of text?
n is small
How about this line of text?

The world smiles with you

import java.util.Scanner;

public class SmileyFace {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        char reply;
        
        System.out.print("Would you like to see a smiley face? ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);
        if (reply == 'Y') {
            System.out.println(":-)");
        } else {
            System.out.println(":-(");
        }
        
        keyboard.close();
    }

}

Successive if statements

import java.util.Scanner;

public class SmileyFace2 {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        char reply;
        
        System.out.print("Would you like to see a smiley face? ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);
        if (reply == 'Y') {
            System.out.println(":-)");
        } 
        if (reply == 'N') {
            System.out.println(":-(");
        }
        if (reply == '?') {
            System.out.println(":-|");
        }
        
        keyboard.close();
    }

}

Guessing game

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        Random myRandom = new Random();
        int randomNumber;
        int guess;
        
        randomNumber = myRandom.nextInt(10) + 1;

        System.out.print("Guess a number from 1 to 10: ");
        guess = keyboard.nextInt();
        if (guess == randomNumber) {
            System.out.println("You win!");
        } else {
            System.out.println("You lose.");
        }
        
        keyboard.close();
    }

}

Converting lengths

This problem is a bit tricky. You're reading an int value followed by a letter. For Java to determine where the int value ends, you must have a blank space after the int value. But Java executes the following two statements one after another,
meters = keyboard.nextInt();
convertTo = keyboard.findWithinHorizon(".", 0).charAt(0);
then the second statement (the findWithinHorizon call) gets the blank space. To fix this problem, you insert an extra findWithinHorizon call between the the two statements to pick up that blank space character.
import java.util.Scanner;

public class ConvertingLengths {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        int meters;
        char blankSpace, convertTo;
        
        System.out.print("Enter the number of meters followed by a blank space and then a letter (c or m): ");
        meters = keyboard.nextInt();
        blankSpace = keyboard.findWithinHorizon(".", 0).charAt(0);
        convertTo = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        if (convertTo == 'c') {
            System.out.print(meters * 100);
            System.out.println(" centimeters");
        } 
        if (convertTo == 'm') {
            System.out.print(meters * 1000);
            System.out.println(" millimeters");
        }
        
        keyboard.close();
    }

}

Putting several statements inside an if statement

import java.util.Scanner;

public class Poem {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        char reply;
        
        System.out.print("Would you like to read a poem? (Y/N) ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        if (reply == 'Y') {
            System.out.println("There once was an old man with glasses");
            System.out.println("Who learned about objects and classes.");
            System.out.println("At last, I see!");
            System.out.println("It's not only for me.");
            System.out.println("I'll teach these ideas to the masses!");
        } else {
            System.out.println("Sorry!");
            System.out.println("I thought you were a poetry buff.");
            System.out.println("Maybe you'll want to see the poem the next time you run this program.");
        }
        
        keyboard.close();
    }

}