List of Chapters
Chapter 19: Out of Many, One
In this chapter:
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.
public class PickAnElement {
public static void main(String[] args) {
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]);
}
}
public class PickAnElement {
public static void main(String[] args) {
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(".");
}
}
}
public class DisplayEvens {
public static void main(String[] args) {
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(" ");
}
}
}
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]);
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class OneVacancy {
public static void main(String[] arge) 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();
}
}
import java.util.Scanner;
import static java.lang.System.out;
public class AddGuests {
public static void main(String[] arge) {
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();
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class HowManyGuests {
public static void main(String[] arge) 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();
}
}
public class MeetingRooms {
public static void main(String[] args) {
var roomName = new String[7];
roomName[0] = "Liberty Bell";
roomName[1] = "Mummers";
roomName[2] = "Rocky Balboa";
roomName[3] = "Cheesesteak";
roomName[4] = "Hoagie";
roomName[5] = "Water Ice";
roomName[6] = "El Train";
for (int index = 0; index < roomName.length; index++) {
System.out.println(roomName[index]);
}
}
}
public class Purchase {
double unitPrice;
int quantity;
boolean taxable;
public double getAmount() {
double total = unitPrice * quantity;
if (taxable) {
total = total * 1.05;
}
return total;
}
}
public class MakePurchases {
public static void main(String[] args) {
Purchase[] purchases = new Purchase[3];
purchases[0] = new Purchase();
purchases[0].unitPrice = 5.25;
purchases[0].quantity = 5;
purchases[0].taxable = false;
purchases[1] = new Purchase();
purchases[1].unitPrice = 10.00;
purchases[1].quantity = 2;
purchases[1].taxable = true;
purchases[2] = new Purchase();
purchases[2].unitPrice = 103.77;
purchases[2].quantity = 3;
purchases[2].taxable = false;
for (Purchase purchase : purchases) {
System.out.println(purchase.getAmount());
}
}
}
import java.util.Scanner;
public class WordOrder {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String[] strings = new String[6];
for (int i = 0; i < 6; i++) {
strings[i] = keyboard.next();
}
for (int lineNumber = 0; lineNumber < 6; lineNumber++) {
for (int i = 0; i < 6; i++) {
if (i < lineNumber) {
System.out.print(strings[i + 1] + " ");
} else if (i == lineNumber) {
System.out.print(strings[0] + " ");
} else {
System.out.print(strings[i] + " ");
}
}
System.out.println();
}
keyboard.close();
}
}
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();
}
}
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();
}
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class NameGame {
public static void main(String[] args) throws IOException {
var names = new ArrayList();
var diskScanner = new Scanner(new File("names.txt"));
for (int i = 0; i < 5; i++) {
names.add(diskScanner.nextLine());
}
names.add(0, names.remove(4));
names.add(3, names.remove(2));
display(names);
}
static void display(ArrayList names) {
for (int i = 0; i < 5; i++) {
System.out.println(names.get(i));
}
}
}
import java.util.ArrayList;
public class FindLargest {
public static void main(String[] args) {
var list = new ArrayList();
list.add(85);
list.add(19);
list.add(0);
list.add(103);
list.add(13);
int largest = -100000;
for (Integer number : list) {
if (number > largest) {
largest = number;
}
}
System.out.println(largest);
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Ducks {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
var list = new ArrayList();
list.add("Alan");
list.add("Barry");
list.add("Harriet");
list.add("Jennie");
list.add("Sam");
System.out.print("Enter a word: ");
var word = keyboard.next();
var index = 0;
while (index < list.size() && word.compareToIgnoreCase(list.get(index)) > 0) {
index++;
}
list.add(index, word);
for (String name : list) {
System.out.println(name);
}
}
}