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

Chapter 11: How to Flick a Virtual Switch

In this chapter:

Don't do this at home (or anywhere else)

This switch statement has no break statements. If the value of amount is 50, Java will execute System.out.println("US half dollar"), and then execute System.out.println("US dollar"), and then execute System.out.println("Not a US coin"). To fix the problem, add break statements:
switch (amount) {
case 1:
    System.out.println("US cent");
    break;
case 5:
    System.out.println("US nickel");
    break;
case 10:
    System.out.println("US dime");
    break;
case 25:
    System.out.println("US quarter");
    break;
case 50:
    System.out.println("US half dollar");
    break;
case 100:
    System.out.println("US dollar");
    break;
default:
    System.out.println("Not a US coin");
}

Days of the week

import java.util.Scanner;
import static java.lang.System.out;

class DaysOfTheWeek {

    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);
        int day;
        
        out.print("Which day of the week is it? (1-7) ");
        day  = keyboard.nextInt();

        switch (day) {
        case 1:
            out.println("Sunday");
            break;

        case 2:
            out.println("Monday");
            break;

        case 3:
            out.println("Tuesday");
            break;

        case 4:
            out.println("Wednesday");
            break;

        case 5:
            out.println("Thursday");
            break;

        case 6:
            out.println("Friday");
            break;

        case 7:
            out.println("Saturday");
            break;

        default:
            out.print("That's not the number of a day of the week.");
            break;
        }

        keyboard.close();
    }
}

Time to eat

import java.util.Scanner;
import static java.lang.System.out;

class TimeToEat {

    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);
        int hour;
        
        out.print("What hour of the day is it? (0-23) ");
        hour  = keyboard.nextInt();

        switch (hour) {
        case 6:
        case 7:
        case 8:
            out.println("Breakfast is served.");
            break;

        case 11:
        case 12:
            out.println("Time for lunch.");
            break;

        case 17:
        case 18:
        case 19:
            out.println("It's dinnertime.");
            break;

        default:
            out.print("Sorry, you'll have to wait, or go get a snack.");
            break;
        }

        keyboard.close();
    }
}

A tiny calculator

import static java.lang.System.out;

import java.util.Scanner;

class Calculator {

    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);
        double operand1, operand2;
        char operation;
        
        out.print("First number: ");
        operand1 = keyboard.nextDouble();
        out.print("Second number: ");
        operand2 = keyboard.nextDouble();
        out.print("Operation (+ - * or /): ");
        operation = keyboard.findWithinHorizon(".", 0).charAt(0);
                
        out.print(operand1);
        out.print(" ");
        out.print(operation);
        out.print(" ");
        out.print(operand2);
        out.print(" = ");
        
        switch (operation) {
        case '+':
            out.println(operand1 + operand2);
            break;

        case '-':
            out.println(operand1 - operand2);
            break;

        case '*':
            out.println(operand1 * operand2);
            break;

        case '/':
            out.println(operand1 / operand2);
            break;

        default:
            out.print("You didn't enter one of + - * or /");
            break;
        }

        keyboard.close();
    }
}

Color by numbers

import static java.lang.System.out;

import java.util.Scanner;

class ColorByNumbers {

    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);
        int number;
        
        out.print("Enter a number from 0 to 7: ");
        number  = keyboard.nextInt();

        switch (number) {
        case 0:
            out.println("black");
            break;

        case 1:
            out.println("blue");
            break;

        case 2:
            out.println("green");
            break;

        case 3:
            out.println("cyan");
            break;

        case 4:
            out.println("red");
            break;

        case 5:
            out.println("magenta");
            break;

        case 6:
            out.println("yellow");
            break;

        case 7:
            out.println("white");
            break;

        default:
            out.print("That's not one of the numbers from 0 and 7.");
            break;
        }

        keyboard.close();
    }
}

Dressed to the nines

import java.util.Scanner;

class SmileyFace {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        char reply;
        
        System.out.print("Would you like to see a smiley face? ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);

        System.out.println(reply == 'Y' ? ":-)" : ":-(");
        
        keyboard.close();
    }

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

class GuessingGame {

    public static void main(String[] args) {
        Scanner 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();

        System.out.println(guess == randomNumber ? "You win!" : "You lose.");
        
        keyboard.close();
    }

}
import java.util.Scanner;

class ConvertingLengths {

    public static void main(String[] args) {
        Scanner 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);
        
        System.out.print(convertTo == 'c' ? meters * 100 : "");
        System.out.print(convertTo == 'm' ? meters * 1000 : "");
        System.out.print(convertTo == 'c' ? " centimeters" : "");
        System.out.print(convertTo == 'm' ? " millimeters" : "");
        System.out.print(convertTo != 'c' && convertTo != 'm' ? meters : "");
        System.out.print(convertTo != 'c' && convertTo != 'm' ? " meters" : "");
        
        keyboard.close();
    }

}