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

Chapter 16: Using Loops and Arrays

In this chapter:

Initializing an array

The program in the first part of this exercise has the following output:
9
21
7
When you uncomment the last System.out.println(myArray[6]) call, you get the following message at runtime:
java.lang.ArrayIndexOutOfBoundsException: 6
	at Main.main(Main.java:8)
This message tells you that you can't ask for the value of myArray[6]. In this program, myArray has only six elements, so the elements are named as follows:
myArray[0]
myArray[1]
myArray[2]
myArray[3]
myArray[4]
myArray[5]
When you separate myArray initialization from the myArray declaration, you get the following error message:
Array constants can only be used in initializers
In other words, you can't separate an array's initialization from the array's declaration.

Pick an element

import java.util.Scanner;

public class PickAnElement {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int amounts[] = { 19, 21, 16, 14, 99, 86, 31, 19, 0, 101 };
        int index;
        
        System.out.print("Input a number from 0 to 9: ");
        index = keyboard.nextInt();
        System.out.println(amounts[index]);
        
        keyboard.close();
    }

}

Display the elements

import java.util.Scanner;

public class PickAnElement {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int amounts[] = { 19, 21, 16, 14, 99, 86, 31, 19, 0, 101 };

        for (int i = 0; i < 10; i++) {
            System.out.print("The ");
            System.out.print(i);
            System.out.print(" element's value is ");
            System.out.print(amounts[i]);
            System.out.println(".");
        }
        
        keyboard.close();
    }

}

Display some of the elements

import java.util.Scanner;

public class DisplayEvens {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int amounts[] = { 19, 21, 16, 14, 99, 86, 31, 19, 0, 101 };

        for (int i = 0; i < 10; i += 2) {
            System.out.print(amounts[i]);
            System.out.print(" ");
        }
        
        keyboard.close();
    }

}

Generating squares

public class Main {

    public static void main(String[] args) {
        int squares[] = new int[50];

        for (int i = 0; i < 50; i++) {
            squares[i] = i * i;
        }

        System.out.println(squares[0]);
        System.out.println(squares[1]);
        System.out.println(squares[2]);
        System.out.println(squares[49]);
    }
}

Find one vacancy

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

class OneVacancy {

    public static void main(String args[]) throws FileNotFoundException {
        Scanner diskScanner = new Scanner(new File("occupancy"));
        int guestsIn[];
        guestsIn = new int[10];

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            guestsIn[roomNum] = diskScanner.nextInt();
        }

        int roomNum = 0;
        while (roomNum < 10 && guestsIn[roomNum] != 0) {
            roomNum++;
        }
        
        if (roomNum < 10) {
            System.out.print("Room ");
            System.out.print(roomNum);
            System.out.println(" is vacant.");
        } else {
            System.out.println("No vacancy");
        }

        diskScanner.close();
    }
}

Select a room

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

class AddGuests {

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

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            guestsIn[roomNum] = 0;
        }

        do {
            int whichRoom = 0;
            while (whichRoom < 10 && guestsIn[whichRoom] != 0) {
                whichRoom++;
            }

            if (whichRoom < 10) {
                out.print("Room ");
                out.println(whichRoom);
                out.print("How many guests? ");
                numGuests = keyboard.nextInt();
                guestsIn[whichRoom] = numGuests;
            } else {
                System.out.println("No vacancy");
            }
            
            out.println();
            out.println("Room\tGuests");
            for (int roomNum = 0; roomNum < 10; roomNum++) {
                out.print(roomNum);
                out.print("\t");
                out.println(guestsIn[roomNum]);
            } 
            
            out.println();
            out.print("Do another? ");
        } while (keyboard.findWithinHorizon(".", 0).charAt(0) == 'Y');

        keyboard.close();
    }
}

How many guests?

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

class HowManyGuests {

    public static void main(String args[]) throws FileNotFoundException {
        Scanner diskScanner = new Scanner(new File("occupancy"));
        int guestsIn[];
        guestsIn = new int[10];
        int numberOfGuests = 0;

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            guestsIn[roomNum] = diskScanner.nextInt();
        }

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            numberOfGuests += guestsIn[roomNum];
        }

        System.out.print("There are ");
        System.out.print(numberOfGuests);
        System.out.println(" in the hotel.");
        
        diskScanner.close();
    }
}

Parallel arrays

import java.util.Scanner;

public class ParallelArrays {

    public static void main(String[] args) {
        char[] cipher = { 's', 'f', 'k', 'l', 'd', 'o', 'h', 'z', 'm', 'b',
                't', 'a', 'n', 'g', 'u', 'v', 'i', 'q', 'x', 'w', 'y', 'c',
                'j', 'r', 'p', 'e' };
        char[] plain =  { 'e', 'q', 's', 'f', 'i', 'n', 'h', 'u', 'r', 'k',
                'g', 'z', 'c', 'y', 'x', 'l', 'm', 'd', 'w', 'a', 'b', 't',
                'p', 'j', 'v', 'o' };
        
        Scanner keyboard = new Scanner(System.in);
        
        char input = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        int index = 0;
        while (index < 26 && cipher[index] != input) {
            index++;
        }
        
        if (index < 26) {
            System.out.println(plain[index]);
        }
        
        keyboard.close();

    }

}

Deciphering ciphertext

import java.util.Scanner;

public class Ciphertext {

    public static void main(String[] args) {
        char[] cipher = { 's', 'f', 'k', 'l', 'd', 'o', 'h', 'z', 'm', 'b',
                't', 'a', 'n', 'g', 'u', 'v', 'i', 'q', 'x', 'w', 'y', 'c',
                'j', 'r', 'p', 'e' };
        char[] plain =  { 'e', 'q', 's', 'f', 'i', 'n', 'h', 'u', 'r', 'k',
                'g', 'z', 'c', 'y', 'x', 'l', 'm', 'd', 'w', 'a', 'b', 't',
                'p', 'j', 'v', 'o' };
        
        Scanner keyboard = new Scanner(System.in);
        
        char input = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        while (input != ' ') {
            int index = 0;
            while (index < 26 && cipher[index] != input) {
                index++;
            }

            if (index < 26) {
                System.out.print(plain[index]);
            }
            input = keyboard.findWithinHorizon(".", 0).charAt(0);
        }

        keyboard.close();

    }

}