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

Chapter 17: How to Flick a Virtual Switch

In this chapter:

Days of the week

import java.util.Scanner;

import static java.lang.System.out;

public class DaysOfTheWeek {

    public static void main(String[] args) {
        var 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");
            case 2 -> out.println("Monday");
            case 3 -> out.println("Tuesday");
            case 4 -> out.println("Wednesday");
            case 5 -> out.println("Thursday");
            case 6 -> out.println("Friday");
            case 7 -> out.println("Saturday");
            default -> out.print("That's not the number of a day of the week.");
        }

        keyboard.close();
    }
}

Time to eat

import java.util.Scanner;

import static java.lang.System.out;

public class TimeToEat {

    public static void main(String[] args) {
        var 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, 7, 8 -> out.println("Breakfast is served.");
            case 11, 12 -> out.println("Time for lunch.");
            case 17, 18, 19 -> out.println("It's dinnertime.");
            default ->
                out.println("Sorry, you'll have to wait, or go get a snack.");
        }

        keyboard.close();
    }
}

Time to eat in the United States

import java.util.Scanner;

import static java.lang.System.out;

public class TimeToEatUS {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        int hour;
        char morningOrEvening;

        out.print("What hour of the day is it? (11 a.m., 12 p.m.) ");
        hour = keyboard.nextInt();
        morningOrEvening = keyboard.next().charAt(0);

        if (morningOrEvening == 'a') {
            switch (hour) {
                case 6, 7, 8 -> out.println("Breakfast is served.");
                case 11 -> out.println("Time for lunch.");
                default -> out.println("Sorry, you'll have to wait, or go get a snack.");
            }
        } else if (morningOrEvening == 'p') {
            switch (hour) {
                case 12 -> out.println("Time for lunch.");
                case 5, 6, 7 -> out.println("It's dinnertime.");
                default -> out.println("Sorry, you'll have to wait, or go get a snack.");
            }
        } else {
            out.println("Please type a number followed by a blank space");
            out.println("  followed by a.m. or p.m.");
        }

        keyboard.close();
    }
}

A tiny calculator

import static java.lang.System.out;

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        var 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);
            case '-' -> out.println(operand1 - operand2);
            case '*' -> out.println(operand1 * operand2);
            case '/' -> out.println(operand1 / operand2);
            default -> out.print("You didn't enter one of + - * or /");
        }

        keyboard.close();
    }
}

Color by numbers

import java.util.Scanner;

import static java.lang.System.out;

public class ColorByNumbers {

    public static void main(String[] args) {
        var 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");
            case 1 -> out.println("blue");
            case 2 -> out.println("green");
            case 3 -> out.println("cyan");
            case 4 -> out.println("red");
            case 5 -> out.println("magenta");
            case 6 -> out.println("yellow");
            case 7 -> out.println("white");
            default -> out.print("That's not one of the numbers from 0 and 7.");
        }

        keyboard.close();
    }
}

Days of the week with switch expression

import java.util.Scanner;

import static java.lang.System.out;

public class DaysOfTheWeekExpression {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        int day;

        out.print("Which day of the week is it? (1-7) ");
        day = keyboard.nextInt();

        String message = switch (day) {
            case 1 -> "Sunday";
            case 2 -> "Monday";
            case 3 -> "Tuesday";
            case 4 -> "Wednesday";
            case 5 -> "Thursday";
            case 6 -> "Friday";
            case 7 -> "Saturday";
            default -> "That's not the number of a day of the week.";
        };

        out.println(message);

        keyboard.close();
    }
}

Time to eat with switch expression

import java.util.Scanner;

import static java.lang.System.out;

public class TimeToEatExpression {

    public static void main(String[] arge) {
        var keyboard = new Scanner(System.in);
        int hour;

        out.print("What hour of the day is it? (0-23) ");
        hour = keyboard.nextInt();

        String message = switch (hour) {
            case 6, 7, 8 -> "Breakfast is served.";
            case 11, 12 -> "Time for lunch.";
            case 17, 18, 19 -> "It's dinnertime.";
            default -> "Sorry, you'll have to wait, or go get a snack.";
        };

        out.println(message);

        keyboard.close();
    }
}

Time to eat in the United States with yield

import java.util.Scanner;

import static java.lang.System.out;

public class TimeToEatUSYield {

    public static void main(String[] arge) {
        var keyboard = new Scanner(System.in);
        int hour;
        char morningOrEvening;
        String breakfast = "Breakfast is served.";
        String lunch = "Time for lunch.";
        String dinner = "It's dinnertime.";
        String sorry = "Sorry, you'll have to wait, or go get a snack.";
        String badInput = "Please type a number followed by a or p.";

        out.print("What hour of the day is it? (11 a.m., 12 p.m.) ");
        hour = keyboard.nextInt();
        morningOrEvening = keyboard.next().charAt(0);
        boolean isAm = morningOrEvening == 'a';
        boolean isPm = morningOrEvening == 'p';

        if (isAm || isPm) {
            out.println(
                    switch (hour) {
                        case 1, 2, 3, 4, 9, 10 -> sorry;
                        case 5 -> {
                            if (isPm) yield dinner;
                            else yield sorry;
                        }
                        case 6, 7 -> {
                            if (isAm) yield breakfast;
                            else yield dinner;
                        }
                        case 8 -> {
                            if (isAm) yield breakfast;
                            else yield sorry;
                        }
                        case 11 -> {
                            if (isAm) yield lunch;
                            else yield sorry;
                        }
                        case 12 -> {
                            if (isPm) yield lunch;
                            else yield sorry;
                        }
                        default -> badInput;
                    }
            );
        } else {
            out.println(badInput);
        }

        keyboard.close();
    }
}

A tiny calculator with yield

import java.util.Scanner;

import static java.lang.System.out;

public class CalculatorYield {

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

        out.println(switch (operation) {
            case '+' -> {
                yield operand1 + operand2;
            }
            case '-' -> {
                yield operand1 - operand2;
            }
            case '*' -> {
                yield operand1 * operand2;
            }
            case '/' -> {
                yield operand1 / operand2;
            }
            default -> {
                yield "You didn't enter one of + - * or /";
            }
        });

        keyboard.close();
    }
}

Dressed to the nines

import java.util.Scanner;

public 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;

public 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;

public 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();
    }

}